How to convert int[] into List in Java?

后端 未结 20 2499
萌比男神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:44

    If you are using java 8, we can use the stream API to convert it into a list.

    List list = Arrays.stream(arr)     // IntStream 
                                    .boxed()        // Stream
                                    .collect(Collectors.toList());
    

    You can also use the IntStream to convert as well.

    List list = IntStream.of(arr) // return Intstream
                                        .boxed()        // Stream
                                        .collect(Collectors.toList());
    

    There are other external library like guava and apache commons also available convert it.

    cheers.

提交回复
热议问题