2016-10-31 5 views
0

는 I는 중첩의 HashMap를 인쇄 할 : "나는 많은 검색하지만 난 그것에에 getValues ​​()를 사용할 때, 그것은 나에게 알려주기 때문에 내가 정수를 인쇄 할 수있는 방법을 찾을 수 없습니다 Integer를 값으로하여 중첩 된 HashMap을 반복하고 인쇄하는 방법은 무엇입니까?

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

기호를 찾을 수 없습니다 ".

이 내가 할 시도 것입니다 (이것은 정수 값이기 때문에) :

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()); 
    } 
} 

나는 내 두 번째로 getValue()를 사용할 수 없습니다, 그래서 다른 무엇을 사용할 수 있습니까?

미리 감사드립니다. 좋은 하루 되세요. Chris.

답변

0

모든 일 인쇄하는 가장 쉬운 방법 :

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

네, 그것은 이잖아을; toString()은지도의 모든 내용을 포함하는 문자열을 반환합니다. 내부지도 포함!

자신이, 당신은 루프를 사용할 수 있도록하고 싶은 경우

:

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() 
6

getValue()Map.Entry하는 방법이 아닌 Map입니다.

두 번째 루프에서 this.map.getValue().entrySet() 대신 t.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()); 
    } 
} 
+0

Eran 완벽하게 작동합니다. – ChrisBlp

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)); 
      } 
     } 
    } 

는 작동 바랍니다.