sneppets-java8

Java – Convert Integer List to int array

This tutorial shows you how to convert Integer List to int array using different ways.

Convert Integer List to int array

This example shows three different ways to convert List<Integer> to int[] array.

package com.sneppets.example;

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

public class IntegerListToIntArrayExample {
	
	public static void main (String[] args) {
		
		//Create integer list
		List<Integer> integerList = new ArrayList<>();
		integerList.add(1);
		integerList.add(2);
		integerList.add(3);
		integerList.add(4);
		integerList.add(5);
		integerList.add(6);
		integerList.add(7);
		System.out.println("Integer List is :" + integerList);
		
		//i)convert integer list to int array
		System.out.println("Integer List to int[] Array");
		printArray(getIntArray(integerList));
		
		//ii)convert integer list to int array using list iterator
		System.out.println("Integer List to int[] Array using listIterator()");
		printArray(getIntArrayWithListIterator(integerList));
		
		//iii)convert integer list to int array using Java 8 stream API
		System.out.println("Integer List to int[] Array using Java 8 Stream");
		printArray(getIntArrayJava8(integerList));
		
	}

	private static int[] getIntArray(List<Integer> integerList) {
		
		int[] intArray = new int[integerList.size()];
		
		for (int i=0; i<integerList.size(); i++) {
			
			intArray[i] = integerList.get(i).intValue();
		}
		
		return intArray;
	}
	
	private static int[] getIntArrayWithListIterator(List<Integer> integerList) {
		
		int[] intArray = new int[integerList.size()];
		
		ListIterator<Integer> iterator = integerList.listIterator();
		
		for(int i=0; i<intArray.length; i++) {
			
			intArray[i] = iterator.next().intValue();
			
		}
		
		return intArray;
	}
	
	private static int[] getIntArrayJava8(List<Integer> integerList) {
		
		int[] intArray = integerList.stream().mapToInt(i -> i).toArray();
		
		return intArray;
				
	}

        private static void printArray(int[] intArray) {
		for (int element : intArray) {
			System.out.println(element);
		}
	}
   
}

Output

Integer List is :[1, 2, 3, 4, 5, 6, 7]
Integer List to int[] Array
1
2
3
4
5
6
7
Integer List to int[] Array using listIterator()
1
2
3
4
5
6
7
Integer List to int[] Array using Java 8 Stream
1
2
3
4
5
6
7

Using listIterator()

Note, the second way uses listIterator() method of List interface, and in cases where you are using LinkedList it is better to iterate using listIterator() to avoid performance issues.

private static int[] getIntArrayWithListIterator(List<Integer> integerList) {
		
    int[] intArray = new int[integerList.size()];
		
    ListIterator<Integer> iterator = integerList.listIterator();
		
    for(int i=0; i<intArray.length; i++) {
			
        intArray[i] = iterator.next().intValue();
			
    }
		
    return intArray;
}

Java 8 Stream API

And the third way of doing this conversion via Java 8 stream API.

private static int[] getIntArrayJava8(List<Integer> integerList) {
		
    int[] intArray = integerList.stream().mapToInt(i -> i).toArray();
		
    return intArray;
				
}

integerList.stream() returns sequential stream of Integer elements from the specified collection and mapToInt(i -> i) returns an IntStream which is a sequence of primitive int-valued elements by mapping each element to itself and toArray() gets the array of int.

Further Learning

References

guest
0 Comments
Inline Feedbacks
View all comments