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());
 }
 }
asked Feb 6, 2018 at 12:26
4
  • What is MapInsideOfAMap, why you have Entry<Character, Map<Integer, Double>> (Double) in outer loop and Map.Entry<Integer, Integer> (Integer) in inner one? Commented Feb 6, 2018 at 12:30
  • There is no map method called getValue(). Change it to get(outer.getKey()). Also you suddenly switch from <Integer, Double> to <Integer, Integer> with your inner map. You need to be consistent. Commented Feb 6, 2018 at 12:31
  • stackoverflow.com/questions/2774608/… check Commented Feb 6, 2018 at 12:31
  • I've updated the code, sorry about the inconsistencies Commented Feb 6, 2018 at 12:34

7 Answers 7

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());
 }
 }
answered Feb 6, 2018 at 12:37
Sign up to request clarification or add additional context in comments.

2 Comments

`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 yeah, correct, I was only trying to fix the error, I updated my answer.
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());
 }
}
answered Feb 6, 2018 at 12:36

Comments

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}}
answered Feb 6, 2018 at 12:43

Comments

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));
});
answered Feb 6, 2018 at 13:06

2 Comments

Used Java8 Lambda expression
@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.
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;
}
answered Feb 6, 2018 at 13:12

Comments

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,68813 silver badges20 bronze badges
answered Feb 6, 2018 at 12:41

Comments

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.