0 votes
in AWS by
What is the best way to convert a primitive Array to a List in Java 8?

1 Answer

0 votes
by
Prior to Java 8:
int[] nums = {1, 2, 3, 4, 5, 6, 7};

List<Integer> numLst = new ArrayList<>();
for (int n : nums)
{
 numLst.add(n);
}
In Java 8:

In Java 8, we could do the conversion of the Stream using boxing.

//using Arrays.stream() sequential stream with boxed
List<Integer> numsLst = Arrays.stream(nums).boxed().collect(Collectors.toList());

//OR
//By using IntStream.boxed(), convert each element of the stream to an Integer ().
List<Integer> numsLst = IntStream.of(nums).boxed().collect(Collectors.toList());

Related questions

0 votes
asked Aug 4, 2023 in JAVA by DavidAnderson
0 votes
asked Apr 15, 2022 in ReactJS by Robindeniel
...