0

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

2
  • What do you mean by, "userInput is just ArrayList"? Can you show the declaration of userInput? Commented Sep 28, 2021 at 23:43
  • ArrayList<Interger> userInput = new ArrayList<>(); Commented Sep 29, 2021 at 5:30

1 Answer 1

2

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]]}
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

how do I add a new value to the map, Ex :{5=[[1, 2, 3, 4], [1, 2, 3, 4],[4,3,5]]}
using put the same way

Your Answer

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.