With streams added in Java 8 we can write code like:

int[] example1 = list.stream().mapToInt(i->i).toArray();
// OR
int[] example2 = list.stream().mapToInt(Integer::intValue).toArray();

Thought process:

  • The simple Stream#toArray returns an Object[] array, so it is not what we want. Also, Stream#toArray(IntFunction<A[]> generator) which returns A[] doesn't do what we want, because the generic type A can't represent the primitive type int

  • So it would be nice to have some kind of stream which would be designed to handle primitive type int instead of the reference type like Integer, because its toArray method will most likely also return an int[] array (returning something else like Object[] or even boxed Integer[] would be unnatural for int). And fortunately Java 8 has such a stream which is IntStream

  • So now the only thing we need to figure out is how to convert our Stream<Integer> (which will be returned from list.stream()) to that shiny IntStream.

    Quick searching in documentation of Stream while looking for methods which return IntStream points us to our solution which is mapToInt(ToIntFunction<? super T> mapper) method. All we need to do is provide a mapping from Integer to int.

    Since ToIntFunction is functional interface we can also provide its instance via lambda or method reference.

    Anyway to convert Integer to int we can use Integer#intValue so inside mapToInt we can write:

    mapToInt( (Integer i) -> i.intValue() )
    

    (or some may prefer: mapToInt(Integer::intValue).)

    But similar code can be generated using unboxing, since the compiler knows that the result of this lambda must be of type int (the lambda used in mapToInt is an implementation of the ToIntFunction interface which expects as body a method of type: int applyAsInt(T value) which is expected to return an int).

    So we can simply write:

    mapToInt((Integer i)->i)
    

    Also, since the Integer type in (Integer i) can be inferred by the compiler because List<Integer>#stream() returns a Stream<Integer>, we can also skip it which leaves us with

    mapToInt(i -> i)
    
Answer from Pshemo on Stack Overflow
Top answer
1 of 16
1076

With streams added in Java 8 we can write code like:

int[] example1 = list.stream().mapToInt(i->i).toArray();
// OR
int[] example2 = list.stream().mapToInt(Integer::intValue).toArray();

Thought process:

  • The simple Stream#toArray returns an Object[] array, so it is not what we want. Also, Stream#toArray(IntFunction<A[]> generator) which returns A[] doesn't do what we want, because the generic type A can't represent the primitive type int

  • So it would be nice to have some kind of stream which would be designed to handle primitive type int instead of the reference type like Integer, because its toArray method will most likely also return an int[] array (returning something else like Object[] or even boxed Integer[] would be unnatural for int). And fortunately Java 8 has such a stream which is IntStream

  • So now the only thing we need to figure out is how to convert our Stream<Integer> (which will be returned from list.stream()) to that shiny IntStream.

    Quick searching in documentation of Stream while looking for methods which return IntStream points us to our solution which is mapToInt(ToIntFunction<? super T> mapper) method. All we need to do is provide a mapping from Integer to int.

    Since ToIntFunction is functional interface we can also provide its instance via lambda or method reference.

    Anyway to convert Integer to int we can use Integer#intValue so inside mapToInt we can write:

    mapToInt( (Integer i) -> i.intValue() )
    

    (or some may prefer: mapToInt(Integer::intValue).)

    But similar code can be generated using unboxing, since the compiler knows that the result of this lambda must be of type int (the lambda used in mapToInt is an implementation of the ToIntFunction interface which expects as body a method of type: int applyAsInt(T value) which is expected to return an int).

    So we can simply write:

    mapToInt((Integer i)->i)
    

    Also, since the Integer type in (Integer i) can be inferred by the compiler because List<Integer>#stream() returns a Stream<Integer>, we can also skip it which leaves us with

    mapToInt(i -> i)
    
2 of 16
265

Unfortunately, I don't believe there really is a better way of doing this due to the nature of Java's handling of primitive types, boxing, arrays and generics. In particular:

  • List<T>.toArray won't work because there's no conversion from Integer to int
  • You can't use int as a type argument for generics, so it would have to be an int-specific method (or one which used reflection to do nasty trickery).

I believe there are libraries which have autogenerated versions of this kind of method for all the primitive types (i.e. there's a template which is copied for each type). It's ugly, but that's the way it is I'm afraid :(

Even though the Arrays class came out before generics arrived in Java, it would still have to include all the horrible overloads if it were introduced today (assuming you want to use primitive arrays).

Discussions

java - An efficient way to convert List<Integer> to int[] ( array ) without iteration - Stack Overflow
This is one reason internal iterators on collections are preferable in my opinion. 2017-01-08T01:06:48.027Z+00:00 ... I updated the answer to handle both RandomAccess and non-RandomAccess Lists. There is no "guarantee" that the indexed access will be faster so benchmarking for the specific use cases would be the only way to show whether it is faster. docs.oracle.com/javase... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 1, 2017
java - Convert Integer List to int array - Stack Overflow
Is there a way to convert a List of Integers to an Array of int (not Integer)? Something like List to int []? Without looping through the list and manually converting the integer to int. More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 8, 2011
Convert List into an array in Java
As u/durakkiller pointed out, the convenient way to do that is the way you provide yourself. Now let's discuss why Java doesn't come with a handy way to do that conversion. Most likely, because there is no demand. No one ever wants to make such conversions, and you shouldn't either. List to Employe[] -> Dont. Keep the list. Why would you want an array? List to int[] -> If you need an array of ints, start with an array of ints. Don't start with a List. If you don't need an array of ints, start with and keep a List. More on reddit.com
๐ŸŒ r/learnjava
8
1
July 17, 2020
How do I make integer lists in Java?
Java has arrays, like int[], but they are not dynamically resizable, and there are some other idiosyncracies as well. Instead prefer to use the generic List interface. Most often, you will want to specifically use the ArrayList implementation, as this gives you O(1) random access and is the Java equivalent to a Python list. Java generics have the caveat that you can't use them with primitive types like int, so instead you should use a wrapper class like Integer. tl;dr: List More on reddit.com
๐ŸŒ r/learnprogramming
4
0
February 4, 2019
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ java โ€บ how to convert list integer to int in java
How to convert List Integer to int in Java | Edureka Community
October 30, 2018 - I'm new to Java. How can i convert a List to int[] in Java? I'm confused because List.toArray() ... i++) ret[i] = list.get(i); return ret; }
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ convert-integer-list-to-int-array-in-java
Convert Integer List to int Array in Java
September 23, 2022 - Here, we are using toArray() method of ArrayList class to convert the given Integer ArrayList to int array. import java.util.*; public class JavaExample { public static void main(String[] args) { //A List of integers List<Integer> list= new ArrayList<>(); list.add(2); list.add(4); list.add(6); ...
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ convert integer list to an int array in java
Convert Integer List to an int array in Java | Techie Delight
July 7, 2026 - We can use the Stream provided by Java 8 to convert a list of Integer to a primitive integer array in Java. We start by converting given List<Integer> to Stream<Integer> using List.stream() method.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2013 โ€บ 05 โ€บ how-to-convert-list-of-integers-to-int-array-java-example-tips.html
How to Convert List of Integers to Int Array in Java - Example
Though it sound simple, there is no straight forward way to convert a list of Integer object to int array in Java. Only way to do this conversion is by using Java 1.5 foreach loop or ArrayUtils from Apache commons.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 416619 โ€บ java โ€บ Convert-Integer-List-int-array
Convert Integer List to int array (Beginning Java forum at Coderanch)
September 23, 2008 - However, I'd probably approach this problem by creating the array manually and then iterating over the list to populate the array. ... IMHO there is no generic way to do this. Java treats an array of primitives or objects as an Array so autoboxing will not help here. I think there is just no avoiding doing it manually. Tom Silverman: SCJP5, SCJD6, SCWCD5, SCBCD5, IBM-142, ScrumMaster ... If you are simply trying to reduce the verbosity of your code then Integer[] can be coded the same way as int [] for accessing elements.
Find elsewhere
Top answer
1 of 4
13

Given the need to convert from Integer to int, I don't think you're going to find something more efficient than what you have, if I assume you're talking about runtime efficiency.

You might find converting to Integer[] first and then looping might be more efficient (below), but you might not, too. You'd have to test it in your specific scenario and see.

Here's that example:

int size = listResult.size();
int[] result = new int[size];
Integer[] temp = listResult.toArray(new Integer[size]);
for (int n = 0; n < size; ++n) {
    result[n] = temp[n];
}
2 of 4
12

If efficiency is your primary concern, I think you can use your solution and make it more efficient by using an indexed for loop on the listResult if it is RandomAccess. However this makes the code much less readable, and you'd have to benchmark it for your use cases to see if it is more efficient.

public static int[] convertListToArray(List<Integer> listResult) {
    int size = listResult.size();
    int[] result = new int[size];
    if (listResult instanceof RandomAccess)
    {
        for (int i = 0; i < size; i++)
        {
            result[i] = listResult.get(i);
        }
    }
    else
    {
        int i = 0;
        for (int num : listResult) {
            result[i++] = num;
        }
    }
    return result;
}

If you use Java 8 and would like to write less code you can use the Streams library.

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = list.stream().mapToInt(i -> i).toArray();

If you are open to using a third party library, you can Eclipse Collections as follows.

MutableList<Integer> list = Lists.mutable.with(1, 2, 3, 4, 5);
int[] array = list.collectInt(i -> i).toArray();

The following is slightly more code, but is the most efficient solution I could come up with using Eclipse Collections.

MutableList<Integer> list = Lists.mutable.with(1, 2, 3, 4, 5);
int[] array = new int[list.size()];
list.forEachWithIndex((each, index) -> array[index] = each);

If you need to use the java.util.List interface, the ListIterate utility class can be used from Eclipse Collections.

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = new int[list.size()];
ListIterate.forEachWithIndex(list, (each, index) -> array[index] = each);

The ListIterate utility will use different iteration code for RandomAccess lists and non-RandomAccess lists.

The most efficient thing to do would be to change the List<Integer> to a MutableIntList in Eclipse Collections or another library that has support for primitive collections.

Note: I am a committer for Eclipse Collections.

๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how can i convert list to int[] in java?
How can I convert List<Integer> to int[] in Java? | Codemia
January 25, 2025 - Introduced in Java 8, the Stream API can be used to convert a List<Integer> to an int[] efficiently with a clear and concise code.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-convert-List-Integer-to-int-in-Java
How to convert List to int[] in Java?
July 30, 2019 - Home Whiteboard Practice Code Graphing Calculator Online Compilers Articles Tools ... Python TechnologiesDatabasesComputer ProgrammingWeb DevelopmentJava TechnologiesComputer ScienceMobile DevelopmentBig Data & AnalyticsMicrosoft TechnologiesDevOpsLatest TechnologiesMachine LearningDigital MarketingSoftware QualityManagement Tutorials View All Categories ... import java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.add(new Integer(4)); int[] array = new int[list.size()]; for(int i=0;i<array.length;i++) { array[i] = list.get(i); System.out.println(array[i]); } } }
๐ŸŒ
Arpit Adlakha's Blog
aadlakha.hashnode.dev โ€บ arpit adlakha's blog โ€บ #java โ€บ how to convert list to int[] in java using streams
How to convert List<Integer> to int[] in java using streams
September 20, 2021 - So what we are doing in "list.stream()" is creating a Stream of Integers from List of Integers. After creating a stream of "Integer" now we want to convert it to stream of "int" (the primitive type).
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-convert-integer-list-to-int-array-117397
Java Integer List to Int Array Conversion | Programming Tutorials | LabEx
In this step, we'll learn our first method to convert an Integer List to an int array using Java's Stream API.
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ list-to-int-java
Converting List to Integer in Java: A Comprehensive Guide โ€” javaspring.net
When converting a List to an int, it's important to handle the case where the list is empty. If the list is empty, the conversion may not make sense, and you may want to return a default value or throw an appropriate exception. Here is an example of handling an empty list using the loop approach: import java.util.ArrayList; import java.util.List; public class ListToIntEmptyList { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int result; if (list.isEmpty()) { result = 0; } else { result = 0; for (Integer num : list) { result = result * 10 + num; } } System.out.println("Converted integer: " + result); } }
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ how-can-i-convert-list-integer-to-int-in-java
How to Convert List<Integer> to int[] in Java: Efficient Methods and Best Practices โ€” javathinking.com
This blog post dives deep into the various methods to convert a List<Integer> to an int[] array in Java. Weโ€™ll explore manual approaches, Java 8+ stream-based solutions, and utilities from popular libraries like Apache Commons and Google Guava.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ convert list into an array in java
r/learnjava on Reddit: Convert List into an array in Java
July 17, 2020 -

Suppose we have a List<Employee> object and we want to convert it into Employee[ ].

List<Employee> list=new ArrayList<>();

We can convert list to object type array in following ways:

Employee[] empArray = list.toArray(new Employee[0]);

or

Employee[] empArray = new Employee[list.size()];

list.toArray(empArray);

But to convert arrays to primitive types. you have to convert it into following way:-

List<Integer> list = ...;

int[] array = new int[list.size()];

for(int i = 0; i < list.size(); i++) array[i] = list.get(i);

Is there any other way to convert List to its specific type of array?

๐ŸŒ
Ducmanhphan
ducmanhphan.github.io โ€บ 2022-04-17-how-to-convert-int-to-list-integer
How to convert int[] to List of Integer
Using the improved version of Arrays.stream() in Java 16+. List<Integer> data = Arrays.stream(rawData) .boxed() .toList(); int[] rawData = {1, 3, 5}; List<Integer> data = new ArrayList<Integer>(); Collections.addAll(data, Arrays.stream(rawD...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how do i make integer lists in java?
r/learnprogramming on Reddit: How do I make integer lists in Java?
February 4, 2019 -

It seems a lot easier from the very little time I spent on Python before making the jump to Java. I'm taking a college night class on Java, and lists are one thing that are confusing me. How do they work in Java?