Skip to content

Instantly share code, notes, and snippets.

@gbajaj
Created July 4, 2025 23:13
Show Gist options
  • Save gbajaj/25d18673fdb858de8d26135800255b78 to your computer and use it in GitHub Desktop.
Save gbajaj/25d18673fdb858de8d26135800255b78 to your computer and use it in GitHub Desktop.
Convert ArrayList<Integer> to a primitive int[] array
import java.util.ArrayList;
import java.util.Arrays;
// Your ArrayList of Integers
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
// Convert it to a primitive int[] array
int[] primitiveArray = arrayList.stream()
.mapToInt(Integer::intValue)
.toArray();
// The result
System.out.println(Arrays.toString(primitiveArray)); // Output: [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment