How to Convert Integer List to Int Array in Java

Rupam Yadav Feb 02, 2024
  1. Stream().mapToInt() to Convert Integer List to Int Array in Java
  2. ArrayUtils.toPrimitive() to Convert Integer List to Int Array in Java
  3. Guava’s Method to Convert Integer List to Int Array in Java
How to Convert Integer List to Int Array in Java

In this tutorial, we will introduce how we can convert a List<Integer> to int[] in Java. We can see that both of them are of different data types that is ArrayList of Integers and the array of int. The former contains an object datatype i.e. Integer, and the latter is a primitive data type i.e. int.

Stream().mapToInt() to Convert Integer List to Int Array in Java

Stream is introduced in Java 8 with the package java.util.stream. It can help us get our desired results because it includes a method mapToInt() which returns an IntStream (a sequence of primitive int values).

To make it more clear, let’s see it in the example below.

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Integer> numList = new ArrayList<Integer>();
    numList.add(11);
    numList.add(22);
    numList.add(33);
    numList.add(44);
    numList.add(55);

    int[] numArray = numList.stream().mapToInt(i -> i).toArray();

    for (int intValue : numArray) {
      System.out.println(intValue);
    }
  }
}

Output:

int primitive: 11
int primitive: 22
int primitive: 33
int primitive: 44
int primitive: 55

We can double check the data type of numArray by debugging. The below debug output shows that the numList is an ArrayList, while numArray is a primitive int.

numList = {ArrayList @832} size = 5 numArray = {int[5] @833} intValue = 11

ArrayUtils.toPrimitive() to Convert Integer List to Int Array in Java

We have another way of casting a List<Integer> to an int[] type. We will use Apache Common Lang, which is a set of helper methods.

ArrayUtils.toPrimitive() allows us to pass in our numList that gives us the result in int[] datatype.

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;

public class Main {
  public static void main(String[] args) {
    List<Integer> numList = new ArrayList<Integer>();
    numList.add(110);
    numList.add(220);
    numList.add(330);
    numList.add(440);
    numList.add(550);

    int[] intArray = ArrayUtils.toPrimitive(numList.toArray(new Integer[numList.size()]));
    for (int intValue : intArray) {
      System.out.println("int primitive: " + intValue);
    }
  }
}

Output:

int primitive: 110
int primitive: 220
int primitive: 330
int primitive: 440
int primitive: 550

Guava’s Method to Convert Integer List to Int Array in Java

Guava is a library which can help us convert our list of Integers into primitive values of int.

Ints.toArray(ListOfIntegers) is a part of the com.google.common.primitives.Ints package that takes our list as an argument and provides us with the array of ints.

import com.google.common.primitives.Ints;
import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Integer> numList = new ArrayList<Integer>();
    numList.add(110);
    numList.add(220);
    numList.add(330);
    numList.add(440);
    numList.add(550);

    int[] numArray = Ints.toArray(numList);
    for (int intValue : numArray) {
      System.out.println("int primitive: " + intValue);
    }
  }
}

Output:

int primitive: 110
int primitive: 220
int primitive: 330
int primitive: 440
int primitive: 550
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Int

Related Article - Java List

Related Article - Java Array