Stream peek() Method in Java with Examples Last Updated : 05 May, 2024 Comments Improve Suggest changes 6 Likes Like Report In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically 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. This is an intermediate operation, as it creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Syntax: Stream<T> peek(Consumer<? super T> action)Here, Stream is an interface and T is the type of stream element. action is a non-interfering action to perform on the elements as they are consumed from the stream and the function returns the new stream.Now we need to understand the lifecycle of peek() method via its internal working via clean java programs listed below as follows: Note: This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline.Since Java 9, if the number of elements is known in advance and unchanged in the stream, the .peek () statement will not be executed due to performance optimization. It is possible to force its operation by a command (formal) changing the number of elements eg. .filter (x -> true).Using peek without any terminal operation does nothing.Example 1: Java // Java Program to Illustrate peek() Method // of Stream class Without Terminal Operation Count // Importing required classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating a list of Integers List<Integer> list = Arrays.asList(0, 2, 4, 6, 8, 10); // Using peek without any terminal // operation does nothing list.stream().peek(System.out::println); } } Output: From the above output, we can perceive that this piece of code will produce no output Example 2: Java // Java Program to Illustrate peek() Method // of Stream class With the Terminal Operation Count // Importing required classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating a list of the Integers List<Integer> list = Arrays.asList(0, 2, 4, 6, 8, 10); // Using peek with the forEach() method // which is a terminal operation list.stream() .peek(System.out::println) .forEach(x -> {}); } } Output0 2 4 6 8 10 Comment S Sahil_Bansall Follow 6 Improve S Sahil_Bansall Follow 6 Improve Article Tags : Misc Java Technical Scripter Java - util package Java-Functions java-stream Java-Stream interface +3 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like