gpt4 book ai didi

java - Java 中的字符串与 Map 中的键进行比较

转载 作者:行者123 更新时间:2023-12-01 23:54:05 26 4
gpt4 key购买 nike

我有一个集合 Map 作为 (String,String) 和字符串 inputText。您建议扫描 inputText 来检查它是否包含 Map 中的任何键。

例如我有下一个:

    //hashmap is used - I don't need the order
Map<String, String> mapOfStrings = new HashMap<String,String>();
mapOfStrings.put("java","yep, java is very useful!");
mapOfStrings.put("night","night coding is the routine");

String inputText = "what do you think about java?"
String outputText = ""; //here should be an <answer>
Set<String> keys = mapOfStrings.keySet();
for (String key:keys){
String value = mapOfStrings.get(key);
//here it must uderstand, that the inputText contains "java" that equals to
//the key="java" and put in outputText the correspondent value
}

据我所知,这不是 equals() 或compareTo()。也许我应该以某种方式检查 inptuText 中的字符顺序?

问候

最佳答案

您可以使用以下内容:

for (String key:keys){
String value = mapOfStrings.get(key);
//here it must uderstand, that the inputText contains "java" that equals to
//the key="java" and put in outputText the correspondent value
if (inputText.contains(key))
{
outputText = value;
}
}

关于java - Java 中的字符串与 Map 中的键进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com