Created
July 4, 2025 23:13
-
-
Save gbajaj/25d18673fdb858de8d26135800255b78 to your computer and use it in GitHub Desktop.
Convert ArrayList<Integer> to a primitive int[] array
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; | |
// 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