Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Integer List to int array [duplicate]

Tags:

java

core

Is there a way, to convert a List of Integers to Array of ints (not integer). Something like List to int []? Without looping through the list and manually converting the intger to int.

like image 317
Kamal Avatar asked Sep 14 '10 06:09

Kamal


People also ask

How will you convert a list into an array of integers like int []?

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. Now all we need to do is convert Stream<Integer> to int[] .

How do you add an int array to an ArrayList in Java?

Using Collections. addAll() method of java. utils. Collections class: This method takes the ArrayList in which the array values are to be inserted as the first parameter; and the Array whose values are to be used as the second parameter.


4 Answers

You can use the toArray to get an array of Integers, ArrayUtils from the apache commons to convert it to an int[].


List<Integer> integerList = new ArrayList<Integer>();
Integer[] integerArray = integerList.toArray(new Integer[0]);
int[] intArray = ArrayUtils.toPrimitive(integerArray);

Resources :

  • Apache commons - ArrayUtils.toPrimitive(Integer[])
  • Apache commons lang
  • Javadoc - Collection.toArray(T[])

On the same topic :

  • How to convert List to int[] in Java?
like image 85
Colin Hebert Avatar answered Oct 04 '22 02:10

Colin Hebert


I'm sure you can find something in a third-party library, but I don't believe there's anything built into the Java standard libraries.

I suggest you just write a utility function to do it, unless you need lots of similar functionality (in which case it would be worth finding the relevant 3rd party library). Note that you'll need to work out what to do with a null reference in the list, which clearly can't be represented accurately in the int array.

like image 36
Jon Skeet Avatar answered Oct 04 '22 01:10

Jon Skeet


No :)

You need to iterate through the list. It shouldn't be too painful.

like image 32
davmac Avatar answered Oct 04 '22 03:10

davmac


Here is a utility method that converts a Collection of Integers to an array of ints. If the input is null, null is returned. If the input contains any null values, a defensive copy is created, stripping all null values from it. The original collection is left unchanged.

public static int[] toIntArray(final Collection<Integer> data){
    int[] result;
    // null result for null input
    if(data == null){
        result = null;
    // empty array for empty collection
    } else if(data.isEmpty()){
        result = new int[0];
    } else{
        final Collection<Integer> effective;
        // if data contains null make defensive copy
        // and remove null values
        if(data.contains(null)){
            effective = new ArrayList<Integer>(data);
            while(effective.remove(null)){}
        // otherwise use original collection
        }else{
            effective = data;
        }
        result = new int[effective.size()];
        int offset = 0;
        // store values
        for(final Integer i : effective){
            result[offset++] = i.intValue();
        }
    }
    return result;
}

Update: Guava has a one-liner for this functionality:

int[] array = Ints.toArray(data);

Reference:

  • Ints.toArray(Collection<Integer>)
like image 42
Sean Patrick Floyd Avatar answered Oct 04 '22 01:10

Sean Patrick Floyd