0

文字列をハッシュマップのキーと比較したい。ここに記載されている手順を使用しようとしましたマップキーを文字列のリストと比較しますが、うまくいきませんでした。

ハッシュマップには多くのエントリが含まれており、渡す文字列を比較したいと考えています。キーが文字列と一致する場合、そこで停止し、一致した文字列の値を出力する必要があります。以下は私のコードです:

HashMap<String, MyBO> myObjs = MyData.getMyData();
Set<String> keys = myObjs.keySet();
String  id = "ABC";
for(String code: keys) {
    MyBO bo  = myObjs.get(code);
    if(keys.contains(itemId)) {
        System.out.println("Matched key = " + id);
    } else {
        System.out.println("Key not matched with ID");
    }
}
4

4 に答える 4

1

このコードを試して、コード要件に似せてください。うまくいくでしょう

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;
            }
        }
于 2013-09-26T04:04:06.103 に答える
0

探しているものは次のとおりです。コードとの違いに注意してください。

HashMap<String, MyBO> myObjs = MyData.getMyData();
Set<String> keys = myObjs.keySet();
String  id = "ABC";
for(String code: keys) {
    if(code.equals(id) { /* this compares the string of the key to "ABC" */
        System.out.println("Matched key = " + id);
    } else {
        System.out.println("Key not matched with ID");
    }
}

または、次のようにすることもできます。

HashMap<String, MyBO> myObjs = MyData.getMyData();
Set<String> keys = myObjs.keySet();
if(keys.contains("ABC") { /* this checks the set for the value "ABC" */
    System.out.println("Matched key = ABC");
 } else {
    System.out.println("Key not matched with ID");
 }
}
于 2013-09-26T03:58:13.597 に答える