Skip to content
Advertisement

How to initialize List Of List in one line [closed]

How can I initialize a List Of Lists in one line?

List<List<Integer>> list = .....;

Advertisement

Answer

List<List<Integer>> list = Arrays.asList(Arrays.asList(1,2), Arrays.asList(3,4));

In Java 9+ you can replace Arrays.asList() with List.of():

List<List<Integer>> list = List.of(List.of(1,2), List.of(3,4));
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement