Posts

Showing posts with the label hashmap

Printing Values From a HashMap Java

Printing Values From a HashMap Java I have a Java program that I am currently making, and am stuck with printing out the keys and the values associated with each key of my hashMap. This is the HashMap that I have declared currently: HashMap<String, ArrayList<String>> mp = new HashMap<>(); Basically assuming that I have the following code written out mp.put(key1, list.add(value1)); mp.put(key1, list.add(value2)); mp.put(key1, list.add(value3)); mp.put(key2, list.add(value5)); mp.put(key2, list.add(value4)); mp.put(key3, list.add(value6)); I want to print the map out like this: Is there any way I can get that done? Thanks for all the help I can get. Please let me know if any more clarification is needed. mp.computeIfAbsent(key1, k -> new ArrayList<>()).add(value1); – shmosel Jun 29 at 21:38 mp.computeIfAbsent(key1, ...