Skip to content

Instantly share code, notes, and snippets.

@gbajaj
Created July 4, 2025 23:11
Show Gist options
  • Save gbajaj/62132ec695e4bd8aebb73da28a1ea505 to your computer and use it in GitHub Desktop.
Save gbajaj/62132ec695e4bd8aebb73da28a1ea505 to your computer and use it in GitHub Desktop.
Converts int [] to ArrayList<Integer>
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