A quick java program guide to convert a List to Array. Examples using List, Java 8 and Guava library. 3 Ways to convert to list to array.
1. Introduction
In this tutorial, We'll learn how to convert List to Array in java. We are going to show the example programs using List, java 8 and Guava API methods.
2. List.toArray()
List is an interface in java that has a utility method List.toArray() which converts List to Array in a simple way. toArray() method returns Object array Object[]. This converted array will have all the elements of a list with proper sequence from first to the last element.
package com.java.w3schools.blog.list; import java.util.ArrayList; import java.util.List; public class ListToArrayExamples { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("Raghu"); names.add("Raja"); names.add("Ramu"); names.add("Paul"); names.add("Penum"); // converting list of string into string[] array. String[] strArray = (String[]) names.toArray(); System.out.println(strArray); } }
Output:
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap') at com.java.w3schools.blog.list.ListToArrayExamples.main(ListToArrayExamples.java:19)
To get the exact type what List is storing, Need to follow T[] toArray(T[] a) method as below.
String[] stringArrayFromList = names.toArray(new String[names.size()]); System.out.println("Printing the converted array values : "); for (String value : stringArrayFromList) { System.out.println(value); }
Printing the converted array values : Raghu Raja Ramu Paul Penum
String[] stringArray = names.toArray(new String[0]); System.out.println("with string array size 0 : "); for (String value : stringArray) { System.out.println(value); }
Output:
with string array size 0 : Raghu Raja Ramu Paul Penum
3. Java 8 Stream API
List to Array conversion can be done using java 8 stream api. List has a method to convert list to stream using List.stream() and next invoke Stream.toArray() method.
<A> A[] toArray(IntFunction<A[]> generator);
3.1 Using Method References
package com.java.w3schools.blog.list; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class Java8StreamsToListToArrayExamples { public static void main(String[] args) { List<String> fruites = new ArrayList<>(); fruites.add("kiwi"); fruites.add("jack fruit"); fruites.add("apple"); fruites.add("Mango"); fruites.add("Strawberry"); Stream<String> stream = fruites.stream(); String[] fruitesArray = stream.toArray(String[]::new); for (String fruit : fruitesArray) { System.out.println(fruit); } } }
Output:
kiwi
jack fruit
apple
Mango
Strawberry
3.2 Using Lambda Expression
The below lambda expression program generates the same output as above.
String[] lamdaArray = fruites.stream().toArray(n -> new String[n]); for (String fruit : lamdaArray) { System.out.println(fruit); }
4. Guava API
Guava library also provided with classes FluentIterable and Iterables.
4.1 FluentIterable
List<String> list = Arrays.asList("kiwi", "jack fruit", "apple", "Mango", "Strawberry");
String[] stringArray = FluentIterable.from(list).toArray(String.class);
System.out.println(Arrays.toString(stringArray));
4.2 Iterables
List<String> list = Arrays.asList("kiwi", "jack fruit", "apple", "Mango", "Strawberry"); String[] stringArray = Iterables.toArray(list, String.class); System.out.println(Arrays.toString(stringArray));
5. Conclusion
In this article, We've seen how to convert List to Array in 3 ways. Based on your understanding, you can choose the right one.
If you do not understand, please leave your questions in the comments section.
GitHub Link 1
GitHub Link 2
List.toString()
Stream.toString()
Guava API
Guava FluentIterable
Guava Iterables
COMMENTS