Java List to Array Example
In this example, we will see how to convert a List (java.util.List) in Java into an array.
java.util.List
has two methods to do so :
T[] toArray(T[] a)
– returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.Object[] toArray()
– returns an array containing all of the elements in this list in proper sequence (from first to last element).
Both these methods return an array containing all of the elements in this list in proper sequence. The difference between the two methods, as we can see in the method signature, is that List.toArray()
would return a type of Object[]
and toArray(T[] a)
will return the type of the specified array at runtime.
java.util.stream.Stream has two methods which convert a stream into an object array.
Object[] toArray()
– returns an array containing the elements of this stream.A[] toArray(IntFunction<A[]> g)
– returns an array containing the elements of this stream.
Let us see examples of these methods:
1. List toArray() example
In this example, we have to put a cast String[]
to the method in order to convert an Object[]
into a String[]
.
method_1_ListtoArray()
private static void method_1_ListtoArray(List<String> stringList) { // Method 1 toArray() // Returns an array containing all of the elements in this list in proper sequence. Object[] array = stringList.toArray(); System.out.println("*** Method 1 - iterating over the array ***"); for (Object str : array) { System.out.println(str); } }
2. List toArray(T[] a) example
method_2_ListtoArray(T[] a)
private static void method_2_ListtoArray(List<String> stringList) { // Method 2 toArray(T[] a) // we are specifying the size of the array to which will be used String[] stringArray = stringList.toArray(new String[stringList.size()]); System.out.println("*** Method 2 - iterating over the array ***"); for (String string : stringArray) { System.out.println(string); } }
3. The User Class example
In the following example, we will not need a cast as the return type is the same as that of the passed list. This method is used to decide the type of returning array at runtime.
User class
package com.javacodegeeks.example; public class User { private String firstName; private String lastName; public User(String firstName, String lastName) { super(); this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return firstName+" "+lastName; } }
method_3_ListtoArray(T[] a)
private static void method_3_ListtoArray(List<User> userList) { User[] userArray = userList.toArray(new User[userList.size()]); System.out.println("*** Method 3 - iterating over the array ***"); for (User user : userArray) { System.out.println(user.toString()); } }
4. Stream toArray() Example
In this step, I will convert a List
to array using Stream
API’s toArray()
method. One for String
, the other for User
.
method_4_StreamtoArray()
private static void method_4_StreamtoArray(List<String> stringList) { String[] stringArray = stringList.stream().toArray(String[]::new); System.out.println("*** Method 4 - iterating over the array ***"); for (String string : stringArray) { System.out.println(string); } }
method_5_StreamtoArray()
private static void method_5_StreamtoArray(List<User> stringList) { User[] stringArray = stringList.stream().toArray(User[]::new); System.out.println("*** Method 5 - iterating over the array ***"); for (User string : stringArray) { System.out.println(string); } }
5. Stream toArray(IntFunction) Example
In this step, I will convert a List
to array using Stream
‘s toArray(IntFunction)
method.
method_6_StreamtoArray()
private static void method_6_StreamtoArray(List<String> stringList) { String[] stringArray = stringList.stream().toArray(ele -> new String[ele]); System.out.println("*** Method 6 - iterating over the array ***"); for (String string : stringArray) { System.out.println(string); } }
6. Demo
Run as a Java application and capture the output here.
Output
*** Method 1 - iterating over the array *** one two three *** Method 2 - iterating over the array *** one two three *** Method 4 - iterating over the array *** one two three *** Method 6 - iterating over the array *** one two three *** Method 3 - iterating over the array *** ani bha abcd pqrs *** Method 5 - iterating over the array *** ani bha abcd pqrs
7. More articles
8. Download the Source code
That was an article on how to convert a List
in Java into an array.
You can download the full source code of this example here: Java List to Array Example
Last updated on May 13th, 2021