How to convert int[] into List in Java?

后端 未结 20 2128
萌比男神i
萌比男神i 2020-11-22 06:18

How do I convert int[] into List in Java?

Of course, I\'m interested in any other answer than doing it in a loop, item by it

20条回答
  •  孤街浪徒
    2020-11-22 06:56

    In Java 8 with stream:

    int[] ints = {1, 2, 3};
    List list = new ArrayList();
    Collections.addAll(list, Arrays.stream(ints).boxed().toArray(Integer[]::new));
    

    or with Collectors

    List list =  Arrays.stream(ints).boxed().collect(Collectors.toList());
    

提交回复
热议问题