Java Stream peek() Example

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

1. Introduction

This tutorial will cover the peek() method in the Java Stream API. peek() is an intermediate operation that allows you to perform a specified action on each element of a stream as elements are consumed from the resulting stream. Typically, it is used for debugging purposes to view elements without modifying them.

Key Points

1. peek() is used for debugging and is an intermediate operation that 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.

2. It does not consume the stream, elements are "peeked" at on-demand basis.

3. It is commonly used to monitor stream values before and after operations like map() or filter().

2. Program Steps

1. Create a Stream of elements.

2. Use peek() to view elements at various points in the stream pipeline.

3. Collect results to trigger the stream processing.

3. Code Program

import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamPeekExample { public static void main(String[] args) { // Stream of strings Stream<String> stringStream = Stream.of("one", "two", "three", "four", "five"); // Peeking and transforming the stream List<String> result = stringStream .peek(s -> System.out.println("Before filter: " + s)) .filter(s -> s.length() > 3) .peek(s -> System.out.println("After filter: " + s)) .map(String::toUpperCase) .peek(s -> System.out.println("After map: " + s)) .collect(Collectors.toList()); System.out.println("Final result: " + result); } } 

Output:

Before filter: one Before filter: two Before filter: three Before filter: four Before filter: five After filter: three After filter: four After filter: five After map: THREE After map: FOUR After map: FIVE Final result: [THREE, FOUR, FIVE] 

Explanation:

1. Stream.of("one", "two", "three", "four", "five") creates a stream of string elements.

2. peek(s -> System.out.println("Before filter: " + s)) allows viewing each element as they pass through the stream before the filter operation.

3. .filter(s -> s.length() > 3) filters strings based on their length being greater than 3.

4. peek(s -> System.out.println("After filter: " + s)) is used to inspect elements after they have passed through the filter operation.

5. .map(String::toUpperCase) transforms each string to uppercase.

6. peek(s -> System.out.println("After map: " + s)) provides a view of each element after the map operation, showing the uppercase transformation.

7. .collect(Collectors.toList()) triggers the processing of the stream and collects the final results into a list.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare