Convert Java Stream to List

By Arvind Rai, October 17, 2020
On this page we will learn how to convert Java Stream to List. To collect stream into List, we need to use Java Stream.collect that performs a mutable reduction operation on the elements using a Collector.
<R,A> R collect(Collector<? super T,A,R> collector) 
To collect stream into List, we need to pass Collector as argument achieved by calling following methods.
1. Collectors.toList
2. Collectors.toUnmodifiableList
3. Collectors.toCollection
Now let us discuss the use of above methods to convert stream into list.

Collectors.toList

The Collectors.toList returns a Collector that accumulates the input elements into a new List.
public static <T> Collector<T,?,List<T>> toList() 
In the example we are converting stream into list of integer and string.
ExampleToList.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class ExampleToList {
  public static void main(String[] args) {
	List<Integer> list1 = Stream.of(15, 20, 30)
		.collect(Collectors.toList()); 
	System.out.println(list1);

	List<Integer> list2 = IntStream.range(15,  20).boxed()
		.collect(Collectors.toList());
	System.out.println(list2);
	
	List<Integer> list3 = Arrays.asList(2, 4, 6).stream().map(i -> i * 2)
		.collect(Collectors.toList());
	System.out.println(list3);
	
	List<String> list4 = Stream.of("Mahesh", "Krishn", "Shiv")
		.collect(Collectors.toList()); 
	System.out.println(list4);	
  }
} 
Output
[15, 20, 30]
[15, 16, 17, 18, 19]
[4, 8, 12]
[Mahesh, Krishn, Shiv] 

Collectors.toUnmodifiableList

The Collectors.toUnmodifiableList returns a Collector that accumulates the input elements into an unmodifiable List in encounter order.
public static <T> Collector<T,?,List<T>> toUnmodifiableList() 
In the example we are converting stream into unmodifiable list of integer and string.
ExampleToUnmodifiableList.java
package com.concretepage;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ExampleToUnmodifiableList {
  public static void main(String[] args) {
	List<Integer> list1 = Stream.of(10, 15, 20).collect(Collectors.toUnmodifiableList()); 
	System.out.println(list1);
	
	List<Integer> list2 = Stream.of(3, 4, 5).map(i -> i * 2).collect(Collectors.toUnmodifiableList());
	System.out.println(list2);
  }
} 
Output
[10, 15, 20]
[6, 8, 10] 

Collectors.toCollection

The Collectors.toCollection returns a Collector that accumulates the input elements into a new Collection, in encounter order.
public static <T,C extends Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory) 
In the example we will convert Stream into ArrayList and LinkedList.
ExampleToCollection.java
package com.concretepage;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ExampleToCollection {
  public static void main(String[] args) {
	List<Integer> list1 = Stream.of(10, 15, 20).map(i -> i * 2)
		.collect(Collectors.toCollection(ArrayList::new)); 
	System.out.println(list1);
	
	List<String> list2 = Stream.of("Mahesh", "Sohan", "Kishan")
		.collect(Collectors.toCollection(ArrayList::new)); 
	System.out.println(list2);	

	LinkedList<Integer> lnkList = Stream.of(40, 15, 35).sorted()
		.collect(Collectors.toCollection(LinkedList::new)); 
	System.out.println(lnkList);
  }
} 
Output
[20, 30, 40]
[Mahesh, Sohan, Kishan]
[15, 35, 40] 

References

Stream
Collectors
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us