-1

entrySet() を使用して、ネストされたハッシュマップを反復処理しようとしています。マルチハッシュマップはデータと呼ばれ、そこに含まれる多くのハッシュマップの中から値を検索しようとしています。しかし、目的の場所にたどり着いたように見えても、検索用語と一致しません。値を出力すると、検索語が実際に現在ループ中のハッシュマップ内の値の 1 つであることがわかります。ここにコードがあります -

HashMap<String,HashMap<String,String>> data =  driver.data.get(dsName);

for (int i = 0; i < arrWhereValues.length; i++) {
String value = null;
for (Entry<String, HashMap<String, String>> entry : data.entrySet()) {
    System.out.println("VALUE:::" +entry.getValue());
    System.out.println("SEARCH FOR::"+arrWhereValues[i]);
    if(arrWhereValues[i].equals(entry.getKey())){
        value = entry.getKey();
        System.out.println("Value in case 1::" +value);
    }
    else if(entry.getValue().containsValue(arrWhereValues[i])){ //Why doesn't it enter here???
        System.out.println("atleast this much was correct!!");
        System.out.println(entry.getValue().entrySet());
        for (Entry<String, String> v : entry.getValue().entrySet()) { //Find a better way of doing this. 
            if(v.getValue().contains(arrWhereValues[i])){
                value = v.getKey();
                System.out.println("Value in case 2 ::"+value);
            }
        }
    }
}

出力:

VALUE:::{name=Testing User , slug_primary=null, slug_secondary=null}
SEARCH FOR::Testing User

私は何を間違っていますか?

4

1 に答える 1

0

このメソッドのようなソリューションを使用して、すべてのハッシュ マップの内容を出力します。

HashMap を反復処理する

おそらくいくつかのキー/値はあなたが期待するものではありません - "Testing User" の末尾にスペースがあるように見えるという Alex のコメントを参照してください。また、「Testing User」は、検索している外側のハッシュマップではなく、ネストされたハッシュマップのキーの 1 つであるようです。

于 2013-07-22T12:29:33.220 に答える