How to Convert Stream to List in Java
-
Convert Stream to List Using the
collect()
Method in Java -
Convert Stream to List Using
toCollection()
Method in Java -
Convert Stream to List Using
forEach()
Method in Java -
Convert Stream to List Using
toArray()
Method in Java
This tutorial introduces the conversion of the Stream
to a List in Java.
A Stream is a collection of objects. A Stream doesn’t store any data, and therefore it is not a data structure.
The Stream
was added to the Java 8 version, whereas a List is an interface that stores ordered collections. In this tutorial, we will look into converting a Stream to a List.
Java has the following ways using which we can achieve our task:
- Convert using
collect()
method - Convert using
toCollection()
method - Convert using
forEach()
method - Convert using
toArray()
method
Convert Stream to List Using the collect()
Method in Java
The Stream collect()
operation is a terminal operation. A terminal operation means that the Stream is consumed and cannot be used further after this operation.
The syntax of this method is:
collect(Collector<? super T, A, R> collector)
This method takes a Collector
object as an argument.
The Collector class is used to collect the elements of the Stream into a collection. This class has the toList()
method, which converts the Stream to a List.
Let’s now look at a code example:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SimpleTesting {
public static void main(String args[]) {
// declaring a stream
Stream<String> furnitures =
Stream.of("chair", "dinning table", "study table", "coffee table", "sofa");
// changing stream to list
List<String> furniture_list = furnitures.collect(Collectors.toList());
// printing the list
for (String furniture : furniture_list) {
System.out.println(furniture);
}
}
}
Output:
chair
dinning table
study table
coffee table
sofa
Convert Stream to List Using toCollection()
Method in Java
This example is similar to the previous one except that we used the Collector.toList()
method instead of the Collectors.toCollection()
method.
The Collector
class has a method called toCollection()
which returns a Collector
that gathers the input items into a new Collection in the order they were encountered. Look at the example code below:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SimpleTesting {
public static void main(String args[]) {
// declaring a stream
Stream<String> furnitures =
Stream.of("chair", "dinning table", "study table", "coffee table", "sofa");
// changing stream to list
List<String> furniture_list = furnitures.collect(Collectors.toCollection(ArrayList::new));
// printing the list
for (String furniture : furniture_list) {
System.out.println(furniture);
}
}
}
Output:
chair
dinning table
study table
coffee table
sofa
Convert Stream to List Using forEach()
Method in Java
In this example, we first created an empty ArrayList then used the forEach()
method to add each Stream element to the List one by one. The Stream
has a method called forEach()
that performs on all the elements of the input Stream.
Look at the example code below:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SimpleTesting {
public static void main(String args[]) {
// declaring a stream
Stream<String> furnitures =
Stream.of("chair", "dinning table", "study table", "coffee table", "sofa");
// declaring an empty arraylist
ArrayList<String> furniture_list = new ArrayList<>();
// adding elements one by one
furnitures.forEach(furniture_list::add);
// printing the list
for (String furniture : furniture_list) {
System.out.println(furniture);
}
}
}
Output:
chair
dinning table
study table
coffee table
sofa
Convert Stream to List Using toArray()
Method in Java
In this example, we first converted the Stream
into an array using the toArray()
method. After this, we converted the newly created array to a List using the asList()
method to get a list.
Look at the code below:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SimpleTesting {
public static void main(String args[]) {
// declaring a stream
Stream<String> furnitures =
Stream.of("chair", "dinning table", "study table", "coffee table", "sofa");
// converting stream to array
String[] furniture_array = furnitures.toArray(String[] ::new);
// converting array to list
List<String> furniture_list = Arrays.asList(furniture_array);
// printing the list
for (String furniture : furniture_list) {
System.out.println(furniture);
}
}
}
Output:
chair
dinning table
study table
coffee table
sofa
Related Article - Java Stream
- How to Convert Iterable to Stream in Java
- How to Distinct by Property in Java 8 Stream
- How to Convert Stream Element to Map in Java
- BiFunction Interface in Java
- The Stream Reduce Operation in Java
- How to Convert Stream to Array in Java