0

ネストされた HashMap を印刷したい:

HashMap<Integer,HashMap<Character,Integer>> map;

よく検索しましたが、整数を出力する方法が見つかりません。これは、getValues() を使用すると、「シンボルが見つかりません」と表示されるためです。(整数値なので)

これは私がやろうとしたことです:

public void print(){
   for(Map.Entry<Integer, HashMap<Character,Integer>> t :this.map.entrySet()){
     Integer key = t.getKey();
     for (Map.Entry<Character,Integer> e : this.map.getValue().entrySet())
       System.out.println("OuterKey:" + key + " InnerKey: " + e.getKey()+ " VALUE:" +e.getValue());
   }
}

2 番目の for で getValue() を使用できないため、他に何が使用できますか?

前もって感謝します !良い1日を。クリス。

4

3 に答える 3

6

getValue()の方法でありMap.Entry、 ではありませんMap

2 番目のループではt.getValue().entrySet()代わりに使用する必要があります。this.map.getValue().entrySet()

これにより、内側のマップが得られます。

public void print(){
   for(Map.Entry<Integer, HashMap<Character,Integer>> t :this.map.entrySet()){
     Integer key = t.getKey();
     for (Map.Entry<Character,Integer> e : t.getValue().entrySet())
       System.out.println("OuterKey:" + key + " InnerKey: " + e.getKey()+ " VALUE:" +e.getValue());
   }
}
于 2016-10-31T09:12:50.183 に答える
0

全体を印刷する最も簡単な方法:

System.out.println(map.toString());

うん、それだけです。toString() は、Map のすべてのコンテンツを含む文字列を返します。その内部マップを含む!

自分でやりたい場合は、次の 2 つのループを使用できます。

for(Map.Entry<Integer, HashMap<Character,Integer>> innerMap : map.entrySet()) {
  for (Map.Entry<Character, Integer> aMap : innerMap.entrySet) {
    ... now you can call aMap.getKey() and .getValue()
于 2016-10-31T09:12:34.843 に答える
0
public static void main(String[] args) {
        HashMap<Integer,HashMap<Character,Integer>> map = new HashMap<Integer,HashMap<Character,Integer>>();
        HashMap<Character,Integer> map1 = new HashMap<Character,Integer>();
        map1.put('1', 11);
        HashMap<Character,Integer> map2 = new HashMap<Character,Integer>();
        map2.put('2', 22);
        map.put(111, map1);
        map.put(222, map2);

        for (Integer temp : map.keySet()) {
            for (Character c : map.get(temp).keySet()) {
                System.out.println("key--" + c + "--value--" + map.get(temp).get(c));
            }
        }
    }

それがうまくいくことを願っています。

于 2016-10-31T09:22:10.340 に答える