1

私は次のyamlデータを持っています

networkLayerConfig: 
    eventMap: !!map
      !!int 100 : 
        !!int 1 : IDLING
        !!int 2 : ACCEPTING

yamlbean を使用して、このデータを次のクラスに読み取ります

class NetworkLayerConfig {
    private HashMap<Integer, Map<Integer, String>> eventMap;
}

すべてのキーとそれに関連付けられた値のペアにアクセスしようとしています。

Javaクラス自体で宣言されているときにこれらのデータにアクセスしようとすると

myHashMap = new HashMap<Integer, Map<Integer, String>>();
myHashMap.put(100, new HashMap<Integer, String>());
myHashMap.get(100).put(1, "IDLING");
myHashMap.get(100).put(2, "ACCEPTING");

この次のコードを使用して

for (Map.Entry<Integer, String> oneMap : myHashMap.get(100).entrySet()) {
    System.out.println(oneMap.getKey());
    System.out.println(oneMap.getValue());
}

yaml ファイルからのみ値 100 を読み取る必要があるため (つまり、実行時)、すべてのデータにアクセスする方法がわかりません。

4

1 に答える 1

1

この問題を解決して、ハッシュマップのすべての値にアクセスしました

Iterator<?> iterator = myparentHashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry myEntry = (Entry) iterator.next();
            System.out.println(myEntry.getKey() + " : ");
            HashMap<Integer, String> childHashMaps = ((HashMap<Integer, String>) myEntry.getValue());
            System.out.println("{");
            for (Map.Entry<Integer, String> childHashMap : childHashMaps.entrySet()) {
                System.out.print(childHashMap.getKey() + " : ");
                System.out.println(childHashMap.getValue());
            }
            System.out.println("}");
        }
于 2012-06-06T14:58:50.207 に答える