如何使用 ArrayList<ArrayList 创建 HashMap<Integer>>



我正在尝试创建一个HashMap,它将看起来像这样

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

我以数组列表的形式从用户那里获得这些值。如何将值添加到映射

i声明映射为

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

我尝试插入像

这样的值
map.put(i,new ArrayList<ArrayList<Integer>>>(userInput));

但是我得到了一个错误,因为userInput只是ArrayList

当使用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]]}
}
}

最新更新