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

时间:2013-04-07 19:53:37

标签: java string collections map

我有一个集合Map as(String,String)和String inputText。 如果它包含来自Map的任何键,你可以建议扫描inputText进行检查。

E.g。我有下一个:

    //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中字符的顺序?

此致

1 个答案:

答案 0 :(得分:3)

您可以使用以下内容:

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;
        }
    }