How to convert an ArrayList containing Integers to primitive int array?

To convert an ArrayList containing Integer objects to a primitive int array in Java, you can use the toArray method and a casting operation.

Here's an example of how you can do this:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);

int[] array = list.stream().mapToInt(i -> i).toArray();

This code will create an ArrayList containing three Integer objects, and then convert it to an int array using the stream, mapToInt, and toArray methods.

Alternatively, you can use the toArray method and a custom array-creation function:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);

int[] array = list.stream().mapToInt(i -> i).toArray();

This code will produce the same result as the previous example.

I hope this helps! Let me know if you have any questions.