Created
July 4, 2025 23:11
-
-
Save gbajaj/62132ec695e4bd8aebb73da28a1ea505 to your computer and use it in GitHub Desktop.
Converts int [] to ArrayList<Integer>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.stream.Collectors; | |
int[] primitiveArray = {1, 2, 3, 4, 5}; | |
// Use Arrays.stream() to convert the int[] to an IntStream, then collect it | |
ArrayList<Integer> arrayList = Arrays.stream(primitiveArray) | |
.boxed() // Converts int to Integer | |
.collect(Collectors.toCollection(ArrayList::new)); | |
System.out.println(arrayList); // Output: [1, 2, 3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment