4

I am trying to print out a hashmap that contains A character as the key and the value as another hashmap with Integer and Double I have this so far but isn't working.

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());
        }
    }
2345678
  • 69
  • 7
  • What is `MapInsideOfAMap`, why you have `Entry>` (Double) in outer loop and `Map.Entry` (Integer) in inner one? – Betlista Feb 06 '18 at 12:30
  • There is no map method called `getValue()`. Change it to `get(outer.getKey())`. Also you suddenly switch from `` to `` with your inner map. You need to be consistent. – OH GOD SPIDERS Feb 06 '18 at 12:31
  • https://stackoverflow.com/questions/2774608/how-do-i-access-nested-hashmaps-in-java/2774626 check – xMilos Feb 06 '18 at 12:31
  • I've updated the code, sorry about the inconsistencies – 2345678 Feb 06 '18 at 12:34

7 Answers7

2

Let's assume your map looks like this:

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

then you can print your map like this:

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());
    }
}
osanger
  • 2,276
  • 3
  • 28
  • 35
2

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

Okay, I understood what you were trying to do, since you already got Outer map entry, you don't have to again use outer map reference, you can directly do like this,

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());
        }
    }
chaitanya89
  • 827
  • 3
  • 20
  • 26
  • `Valid soultion. but in my opionion you should avoid using ``MapInsideOfAMap.get(outer.getKey()).entrySet()`` ervery iteration. even if its O(1). But +1 – osanger Feb 06 '18 at 12:50
  • 1
    @osanger yeah, correct, I was only trying to fix the error, I updated my answer. – chaitanya89 Feb 06 '18 at 12:52
2

If you need just to see map key/values, use System.out.println

Map AbstractMap.toString knows how to print itself in a nice and readable way.

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

prints out this :

{A={1=0.01}, B={2=0.02}}
yvoytovych
  • 871
  • 4
  • 12
0

In the second for-loop, you need to access the Map you get as a value from the outer loop. You also need to change the type of the Entry 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());
    }
}
amain
  • 1,668
  • 13
  • 19
Kiran H M
  • 1
  • 1
0

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

To simply print out the whole map of maps:

System.out.println(mapInsideOfAMap);

Now, if you want to iterate your outer and inner maps and print their key/value pairs you could use the Map.forEach method:

mapInsideOfAMap.forEach((outerKey, outerValue) -> {
    System.out.println("Char: " + outerKey + "\n");
    outerValue.forEach((innerKey, innerValue) -> 
        System.out.println("int = " + innerKey + ", double = " + innerValue));
});
fps
  • 33,623
  • 8
  • 55
  • 110
  • Used Java8 Lambda expression – Pankaj Feb 06 '18 at 13:14
  • @Pankaj Yes, `Map.forEach` expects a `BiConsumer` that receives the key and the value, and this `BiConsumer` can be the target of a lambda expression to make the code more succint. – fps Feb 06 '18 at 13:17
0
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;
}
Pankaj
  • 1,242
  • 1
  • 9
  • 21