Stream peek(Consumer action) example

Description

Stream peek(Consumer<? super T> action) returns a stream consisting of the elements of this stream, performing the provided action on each element.

Syntax

peek has the following syntax.


Stream<T> peek(Consumer<? super T> action)

Example

The following example shows how to use peek.


import java.util.Arrays;
import java.util.List;
//  w w  w.  j  a  v  a  2 s  .co  m
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("1","1","2","3","4");

    stringList.stream()      
              .limit(2)
              .peek(System.out::println);
  }
}




















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder