user10183910
user10183910

Reputation: 21

How to create a HashMap with ArrayList<ArrayList<Integer>>

I am trying to create a HashMap which will look like this

key.   value
1.     [[1,2,5][4,5]]
2.     [[12,2][45,54]]
3.     [[1,23][43,25]]

I get these values from the user as a ArrayList. How do I add the values to the map

i declared tha map as

HashMap<Integer,ArrayList<ArrayList<Integer>>> map = new HashMap<>();

I tried to insert the value like

map.put(i,new ArrayList<ArrayList<Integer>>>(userInput));

but I get a error since userInput is just ArrayList

Upvotes: 0

Views: 54

Answers (1)

MaxG
MaxG

Reputation: 1077

This works when working with java 11:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
      List<Integer> inner = List.of(1, 2, 3, 4);
      List<List<Integer>> outer = List.of(inner, inner);
      
      Map<Integer, List<List<Integer>>> map = Map.of(5, outer);
      
      System.out.println(map); // {5=[[1, 2, 3, 4], [1, 2, 3, 4]]}
    }
}

Upvotes: 2

Related Questions