Since keys and values in a hash map can be any variable, it is also possible to use lists as values in a hash map. You can add more values to a single key by attaching a list to the key.
HashMap<String, ArrayList<String>> phoneNumbers = new HashMap<>();
Each key of the hash map now has a list attached to it. Although the new command creates a hash map, the hash map initially does not contain a single list. They need to be created separately as needed.
HashMap<String, ArrayList<String>> phoneNumbers = new HashMap<>();
// let's initially attatch an empty list to the name Pekka
phoneNumbers.put("Pekka", new ArrayList<>());
// and add a phone number to the list at Pekka
phoneNumbers.get("Pekka").add("040-12348765");
// and let's add another phone number
phoneNumbers.get("Pekka").add("09-111333");
System.out.println("Pekka's numbers: " + phoneNumbers.get("Pekka"));
putIfAbsent
is used to put if the key is not present.