Convert Java Stream to List
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)
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
TheCollectors.toList
returns a Collector
that accumulates the input elements into a new List
.
public static <T> Collector<T,?,List<T>> toList()
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); } }
[15, 20, 30] [15, 16, 17, 18, 19] [4, 8, 12] [Mahesh, Krishn, Shiv]
Collectors.toUnmodifiableList
TheCollectors.toUnmodifiableList
returns a Collector
that accumulates the input elements into an unmodifiable List
in encounter order.
public static <T> Collector<T,?,List<T>> toUnmodifiableList()
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); } }
[10, 15, 20] [6, 8, 10]
Collectors.toCollection
TheCollectors.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)
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); } }
[20, 30, 40] [Mahesh, Sohan, Kishan] [15, 35, 40]
References
StreamCollectors