Java Stream peek() with examples

 205 total views,  2 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream peek() operation with examples.

peek() is a part of Stream pipeline and an intermediate operation. It belongs to the Stream interface.

Java stream peek() operation allows us to visualize the data or see the elements of the stream for debugging as they flow.

peek() should be mainly used for debugging the stream.

peek() should not used to transform or alter the data. Instead, it is can be used to visualize the elements after a each intermediate operation is applied. i.e to debug each element of the stream.

Method description

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

As per official documentation

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

peek() operation will always create a new stream.


peek() accepts a consumer operation to be performed on the stream. A block of code can be passed to peek() to evaluate or visualize each element.

peek() method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline

Let us now see a few examples of Java 8 stream peek()

Java stream peek() operation example

import java.util.ArrayList;
import java.util.List;

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("orange");
        list.add("banana");
        list.add("Guava");
        list.add("plums");
        list.add("peach");
        list.add("cherry");
        list.add("jackfruit");

        list.stream() // convert list to stream
                .map(i -> i + i) // Map input = input+input
                .peek(StaticReference::print) // Peeking each element
                .map(i -> i + 1) // Map input = input + 1
                .forEach(System.out::println); // print each element
    }

    static void print(String input)
    {
        System.out.println("Peeking " + input);
    }

}

Output

Peeking appleapple
appleapple1
Peeking orangeorange
orangeorange1
Peeking bananabanana
bananabanana1
Peeking GuavaGuava
GuavaGuava1
Peeking plumsplums
plumsplums1
Peeking peachpeach
peachpeach1
Peeking cherrycherry
cherrycherry1
Peeking jackfruitjackfruit
jackfruitjackfruit1

Conclusion

peek() operation is a useful intermediate operation to analyze or debug flow of the Stream. It accepts a consumer function which doesn’t return any value but can do some operation on the element for debugging purpose.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.