In Java, there can be certain code functionalities that are not vital for some time span that can be avoided to enhance the overall code performance. More specifically, while dealing with complex codes and those ones that need to be changed more frequently. In such scenarios, the Stream “peek()” method in Java is assistive in streamlining the code performance from time to time.
This blog will demonstrate the usage and working of the Stream “peek()” method in Java.
How to Use/Apply Stream peek() Method in Java?
The Stream “peek()” method corresponds to an intermediate functionality that returns a Stream comprising the elements of the current stream. It additionally performs the specified action on each stream element. This method is implemented to assist in debugging. It is such that if the number of elements is known and unchanged/unmodified in the stream, the “peek()” method is not invoked due to performance optimization. Also, using the “peek()” method without any terminal operation does nothing.
Syntax
Stream<T> peek(Consumer<? super T> act)
In this syntax:
- “Stream” refers to an interface.
- “T” corresponds to the stream item’s type.
- “act” is a non-interfering action to apply to the items.
Example 1: Applying the Stream “peek()” Method in Java
This example implements the “peek()” method without any terminal operation which does nothing on the console/output. It is because an intermediate operation doesn’t run until a terminal operation is executed:
import java.util.*;public class Peek {
public static void main(String[] args){
List<Integer> givenList = Arrays.asList(1, 2, 3, 4);
givenList.stream().peek(System.out::println);
}}
In these code lines:
- Import the stated package to allow working with all the features in the “java.util” package.
- Create a list of “Integer” types consisting of the given integers.
- After that, associate the “peek()” method with the list that does nothing since it is applied without any terminal operation.
Output

As indicated, the output displays nothing upon executing the code.
Example 2: Applying the Stream “peek()” Method in Java With a Terminal Operation
In this specific example, the discussed method can be implemented along with a terminal operation i.e., “collect()”:
import java.util.*;import java.util.stream.Collectors;public class Streampeek {
public static void main(String[] args){
List<Integer> givenList = Arrays.asList(1, 2, 3, 4);
List<Integer> updatedList = givenList.stream().peek(System.out::println)
.collect(Collectors.toList());
System.out.println(updatedList);
}}
According to this block of code:
- Include the stated additional package to work with the “Collectors” class.
- Similarly, define a list of “Integer” types.
- Now, apply the combined “peek()” and “collect()” methods to collect the list elements, store them in the list, and display them.
Output

This output signifies that the list elements are returned appropriately due to the applied terminal operation along with the “peek()” method.
Conclusion
The Java Stream “peek()” method is an intermediate operation that gives a Stream comprising the elements of the current stream and returns nothing if applied without a terminal operation. This article elaborated on the Stream “peek()” method.