Java 8 – Collect Stream Items into List

Learn different ways to convert a given Java Stream to List using Collectors.toList() and Collectors.toCollection() APIs.

1. Converting Stream to List

In the given example, we have a stream of String tokens. We want to store all the tokens from the stream into an List.

In such simple usecases, Stream.collect() API is best suited along with Collectors.toList().

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{	
		Stream<String> tokenStream = Stream.of("A", "B", "C", "D");	//Stream
		
		List<String> tokenList = tokenStream.collect(Collectors.toList()); 	//Stream -> List

		System.out.println(tokenList);	
	}
}

Program output.

[A, B, C, D]

2. Collecting Stream into LinkedList

The given example is only slightly different from the first example. Here, we are collecting the Stream items into a LinkedList.

In this example, we are using Collectors.toCollection(LinkedList::new) API along with Stream.collect() API.

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{	
		Stream<String> tokenStream = Arrays.asList("A", "B", "C", "D").stream();	//Stream
		
		List<String> tokenList = tokenStream
                    .collect(Collectors.toCollection(LinkedList::new)); 	//Stream -> LinkedList

		System.out.println(tokenList);	
	}
}

Program output.

[A, B, C, D]

3. Filtering Stream Items and Collecting into List

Sometimes we need to find only specific items from the Stream and then add only those items to List. Here, we can use Stream.filter() method to pass a predicate which will return only those items which match the given pre-condition.

In the given example, we are filtering all employees whose salary is less than 400. Then we are collecting those employees into a List.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{
		Stream<Employee> employeeStream = Stream.of(
							new Employee(1, "A", 100),
							new Employee(2, "B", 200),
							new Employee(3, "C", 300),
							new Employee(4, "D", 400),
							new Employee(5, "E", 500),
							new Employee(6, "F", 600));
		
		List<Employee> employeeList = employeeStream
						.filter(e -> e.getSalary() < 400)
						.collect(Collectors.toList());
		
		employeeList.forEach(System.out::println);
	}
}

Program output.

Employee [id=1, name=A, salary=100.0]
Employee [id=2, name=B, salary=200.0]
Employee [id=3, name=C, salary=300.0]

4. Collect Items from Infinite Stream into List

To convert an infinite stream into list, we must limit the stream to a finite number of elements. Given example will work in case of stream of primitives.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main 
{
	public static void main(String[] args) 
	{
		IntStream infiniteNumberStream = IntStream.iterate(1, i -> i+1);

		List<Integer> integerlist = infiniteNumberStream.limit(10)
						.boxed()
						.collect(Collectors.toList());

		System.out.println(integerlist);
	}
}

Program output.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

5. Conclusion

Clearly, We can use Collectors.toList() function is variety of ways to collect stream elements into an list in all usescases.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.