Close

Java 8 Streams - Collectors.toList Examples

Java 8 Streams Java Java API 


java.lang.Objectjava.lang.Objectjava.util.stream.Collectorsjava.util.stream.CollectorsLogicBig

The static methods Collectors.toList() returns a Collector which produces a new List instance, populated with stream elements if used as parameter of Stream.collect(Collector) method.

<T> Collector<T,?,List<T>> toList()


Examples


package com.logicbig.example.collectors;

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

public class ToListExample {
public static void main (String[] args) {
Stream<BigDecimal> s = Stream.iterate(
BigDecimal.ONE, bigDecimal ->
bigDecimal.add(BigDecimal.ONE))
.limit(10)
.peek(System.out::println);

List<BigDecimal> l = s.collect(Collectors.toList());
System.out.println(l);
}
}

Output

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




See Also