Hashmap inside hasmap listing

I am trying to print a hashmap that contains the character A as a key, and the value as another hashmap with Integer and Double. It still works for me, but it does not work.

HashMap<Character, Map<Integer, Double>> MapInsideOfAMap = calc.MapInAMap(abc);
    for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
        System.out.println("Char: " + outer.getKey() + "\n");
        for (Map.Entry<Character, Map<Integer, Double> inner : MapInsideOfAMap.getValue().entrySet()) {
            System.out.println("int = " + inner.getKey() + ", double = " + inner.getValue());
        }
    }
+4
source share
7 answers

Suppose your map looks like this:

Map <Character, Map<Integer, Double>> MapInsideOfAMap = new HashMap();

then you can print your card as follows:

for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
    System.out.println("Char: " + outer.getKey() + "\n");
    HashMap<Integer, Double> innermap = MapInsideOfAMap.get(outer.getKey());
    for (Map.Entry<Integer, Double> innerEntry : innermap.entrySet()) {
         System.out.println("int = " + innerEntry.getKey() + ", double = " + innerEntry.getValue());
    }
}
+2
source

Your code should be like this

for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
        System.out.println("Char: " + outer.getKey() + "\n");
        for (Entry<Integer, Double> inner : MapInsideOfAMap.get(outer.getKey()).entrySet()) {
            System.out.println("int = " + inner.getKey() + ", double = " + inner.getValue());
        }
    }

Ok, I understand what you are trying to do, since you already received the Outer map entry, you do not need to use the link to an external map again, you can do it directly,

for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
        System.out.println("Char: " + outer.getKey() + "\n");

        for (Entry<Integer, Double> inner : outer.getValue().entrySet()) {
            System.out.println("int = " + inner.getKey() + ", double = " + inner.getValue());
        }
    }
+2
source

/ , System.out.println

AbstractMap.toString .

Map<Character, Map<Integer, Double>> map = new HashMap<>();
map.put('A', new HashMap<>());
map.get('A').put(1, 0.01);
map.put('B', new HashMap<>());
map.get('B').put(2, 0.02);
System.out.println(map);

:

{A={1=0.01}, B={2=0.02}}
+2

In the second for-loop, you need to access the Map, which you get as the value from the outer loop. You also need to change the record type in the second loop.

Try this code:

for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
    System.out.println("Key: " + outer.getKey() + "\n");
    for (Entry<Integer, Double> inner : outer.getValue().entrySet()) {
        System.out.println("Key = " + inner.getKey() + ", Value = " + inner.getValue());
    }
}
0
source

But the complexity of the structure is probably redundant.

public static void print(char keyData, Map<Character, Map<Integer, Double>> fromData) {
    System.out.println("print: " + get(keyData, fromData));
}

public static Map<Integer, Double> get(char keyData, Map<Character, Map<Integer, Double>> fromData) {

    for (Map.Entry<Character, Map<Integer, Double>> entry : fromData.entrySet()) {

        Character key = entry.getKey();

        if(key.equals(keyData))
            return entry.getValue();

    }

    return Collections.emptyMap();
}
0
source

To simply print the entire map card:

System.out.println(mapInsideOfAMap);

Now, if you want to iterate your external and internal maps and print your key / value pairs, you can use the method Map.forEach

mapInsideOfAMap.forEach((outerKey, outerValue) -> {
    System.out.println("Char: " + outerKey + "\n");
    outerValue.forEach((innerKey, innerValue) -> 
        System.out.println("int = " + innerKey + ", double = " + innerValue));
});
0
source
public static void main(String z[]) {

    Map<Character, Map<Integer, Double>> MapInsideOfAMap = getmapOfMap();
    for (Entry<Character, Map<Integer, Double>> outer : MapInsideOfAMap.entrySet()) {
        System.out.println("Char: " + outer.getKey() + "\n");

        Map<Integer, Double> mapInner = MapInsideOfAMap.get(outer.getKey());
        for (Map.Entry<Integer, Double> inner : mapInner.entrySet()) {
             System.out.println(inner.getKey() +":"+ mapInner.get(inner.getKey()));
        }
    }
}

private static Map getmapOfMap() {
    char[] chArr = {'a','b','c','d','e','f','g','h','i','j','k'};
    HashMap<Character, Map<Integer, Double>> mapInsideOfAMap = new HashMap<Character, Map<Integer, Double>>();

    for(char ch:chArr) {
        mapInsideOfAMap.put(ch, getInnterMap());

    }
    return mapInsideOfAMap;
}

private static Map getInnterMap() {
     Map<Integer, Double> map = new HashMap<>();

     for(int i=1000;i<1010;i++) {
         map.put(i, new Double(String.valueOf(i)));
     }
     return map;
}
0
source

Source: https://habr.com/ru/post/1693248/


All Articles