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());
}
}
7 Answers 7
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());
}
}
2 Comments
MapInsideOfAMap.get(outer.getKey()).entrySet() ervery iteration. even if its O(1). But +1Let'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());
}
}
Comments
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}}
Comments
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));
});
2 Comments
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.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;
}
Comments
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());
}
}
Comments
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();
}
MapInsideOfAMap, why you haveEntry<Character, Map<Integer, Double>>(Double) in outer loop andMap.Entry<Integer, Integer>(Integer) in inner one?getValue(). Change it toget(outer.getKey()). Also you suddenly switch from<Integer, Double>to<Integer, Integer>with your inner map. You need to be consistent.