Convert List to array Java example shows how to convert List to an array in Java including a primitive array using for loop, toArray method, Stream, and Apache Commons.
How to convert List to Array in Java?
The List can be converted to an array using the toArray
method of the List.
1 |
<T> T[] toArray(T[] array) |
This method returns an array containing all the elements of the argument list in the same sequence. The type of array returned from this method is the same as the argument array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.javacodeexamples.basic.conversion; import java.util.ArrayList; import java.util.List; public class ConvertListToArrayExample { public static void main(String[] args) { //create new List List<String> listLanguages = new ArrayList<String>(); //Add elements to the List listLanguages.add("Java"); listLanguages.add("C++"); listLanguages.add("C"); listLanguages.add("Cobol"); listLanguages.add("Fortran"); /* * To convert List to an array * use toArray method of List */ String[] strLanguages = listLanguages.toArray( new String[listLanguages.size()] ); for(String language : strLanguages) System.out.println(language); } } |
Output
1 2 3 4 5 |
Java C++ C Cobol Fortran |
Note:
If you pass an array smaller than the List to the toArray
method, List will still be converted to an array of the correct type, but in that case, a new array will be created by JVM of the same type. It is always suggested to pass an array of the same size to avoid the creation of another array.
How to convert List to an array of primitive types?
The above method works only for the List containing object references. If you want to convert List to an array of primitive types, you need to loop through the elements of the List and fill the array as given in the below example. You can also use the Apache commons library.
a) Using For loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.javacodeexamples.basic.conversion; import java.util.ArrayList; import java.util.List; public class ConvertListToArrayExample { public static void main(String[] args) { List<Integer> listMarks = new ArrayList<Integer>(); listMarks.add(85); listMarks.add(99); listMarks.add(65); listMarks.add(43); listMarks.add(74); //create new int array of same size int[] marks = new int[listMarks.size()]; //loop through the list and fill int array for(int i = 0; i < listMarks.size(); i++) marks[i] = listMarks.get(i); //print array System.out.println("Convert List to primitive array"); for(int mark : marks) System.out.println(mark); } } |
Output
1 2 3 4 5 6 |
Convert List to primitive array 85 99 65 43 74 |
b) Using Apache Commons library
You can use the toPrimitive
method of the ArrayUtils class of the Apache commons library as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
List<Integer> listMarks = new ArrayList<Integer>(); listMarks.add(85); listMarks.add(99); listMarks.add(65); listMarks.add(43); listMarks.add(74); //convert Integer[] to int[] int[] marks = ArrayUtils.toPrimitive( listMarks.toArray( new Integer[listMarks.size()]) ); //print array System.out.println("Convert List to primitive array"); for(int mark : marks) System.out.println(mark); |
Output
1 2 3 4 5 6 |
Convert List to primitive array 85 99 65 43 74 |
How to convert using the Java 8 Stream?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//create new List List<String> listLanguages = new ArrayList<String>(); //Add elements to the List listLanguages.add("Java"); listLanguages.add("C++"); listLanguages.add("C"); listLanguages.add("Cobol"); listLanguages.add("Fortran"); //use Java 8 Stream to convert List to array String[] strLanguages = listLanguages.stream().toArray(String[]::new); //print array for(String language : strLanguages) System.out.println(language); |
Output
1 2 3 4 5 |
Java C++ C Cobol Fortran |
This example is a part of the Java Array tutorial and Java ArrayList tutorial.
Please let me know your views in the comments section below.