How to fill a hashmap with an arraylist?

Jam*_*sig 0 java arraylist hashmap

It kinda speaks for itself but how do I fill this?

Map<Integer,ArrayList<Integer>>intMap = new HashMap<Integer, ArrayList<Integer>>();
Run Code Online (Sandbox Code Playgroud)

I've already tried

intMap.put(1, 2);
intMap.put(1, 3); etc
Run Code Online (Sandbox Code Playgroud)

and

intMap.put(1, (2, 3);
Run Code Online (Sandbox Code Playgroud)

Fed*_*ner 5

You should use Map.computeIfAbsent:

intMap.computeIfAbsent(someKey, k -> new ArrayList<>()).add(someValue);
Run Code Online (Sandbox Code Playgroud)

For example, to have these mappings:

1 -> [2, 3]
5 -> [8]
6 -> [7, 9, 4]
Run Code Online (Sandbox Code Playgroud)

You could do it this way:

intMap.computeIfAbsent(1, k -> new ArrayList<>()).add(2);
intMap.computeIfAbsent(1, k -> new ArrayList<>()).add(3);

intMap.computeIfAbsent(5, k -> new ArrayList<>()).add(8);

intMap.computeIfAbsent(6, k -> new ArrayList<>()).add(7);
intMap.computeIfAbsent(6, k -> new ArrayList<>()).add(9);
intMap.computeIfAbsent(6, k -> new ArrayList<>()).add(4);
Run Code Online (Sandbox Code Playgroud)

EDIT:

Map.computeIfAbsent is equivalent to this code:

List<Integer> list = intMap.get(someKey);
if (list == null) {
    list = new ArrayList<>();
    intMap.put(someKey, list);
}
list.add(someValue);
Run Code Online (Sandbox Code Playgroud)