1

こんにちは、次のようなネストされた Map のキーと値を保存したいと思いました。

Map<ArrayList <String>, Map<String, Integer>> NestedMap = new HashMap<ArrayList<String>, Map<String, Integer>();

別の変数に getKeyFromInsideMap と getValueFromInsideMap としましょう。したがって、アクセスに関心があるのは、内側の Map String と Integer の値になります。コードでこれを行うにはどうすればよいですか?

ここのフォーラムでいくつかの例を試しましたが、構文がどのようになるかわかりません。このためのコードを教えてください。ありがとうございました!

4

1 に答える 1

4

ネストされていない Map から値を取得するのと同じ方法で、ネストされた by Map から値を取得します。同じプロセスを 2 回適用するだけです。

//Java7 Diamond Notation
Map<ArrayList, Map<String, Integer>> nestedMap = new HashMap<>();

//get nested map 
Map<String, Integer> innerMap = nestedMap.get(some_key_value_string);

//now get the Integer value from the innerMap
Integer innerMapValue = innerMap.get(some_key_value_string);

また、特定のキーを探している場合は、次のようにマップを反復処理できます。

Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    System.out.println("Key: " + pairs.getKey() + " Val: " + pairs.getValue()));
    it.remove(); // avoids a ConcurrentModificationException
}

これは、単一のマップのすべてのキーと値を反復処理します。

お役に立てれば。

于 2012-05-15T19:16:11.710 に答える