DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • How to Get Word Document Form Values Using Java
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • Exploring Embeddings API With Java and Spring AI

Trending

  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • How to Format Articles for DZone
  1. DZone
  2. Coding
  3. Java
  4. Exploring Java Stream API's peek Method

Exploring Java Stream API's peek Method

In this article, learn more about the peek method, a powerful and often underutilized feature in the Java Stream API.

By 
Sameer Shukla user avatar
Sameer Shukla
DZone Core CORE ·
Jan. 02, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.2K Views

Join the DZone community and get the full member experience.

Join For Free

The peek method is a powerful and often underutilized feature in the Java Stream API. The peek method is introduced to provide a mechanism for debugging and gaining insights into intermediate stages of stream pipelines and offers a window into the transformation of the data facilitating a clear understanding of the flow of elements within the stream.

Basic Syntax

The basic syntax of the peek method involves inserting it into a stream pipeline, typically before a terminal operation. Its usage can be illustrated as follows:

Java
 
List<String> words = List.of("apple", "banana", "orange", "grape");

words.stream()
        .peek(word -> System.out.println("Processing: " + word))
        .map(String::toUpperCase)
        .forEach(System.out::println);


In this example, the peek method logs each element before it undergoes the map operation.

Benefits and Use Cases

1. Observational Logging

peek allows developers to insert logging statements to observe elements at various stages of the stream pipeline. It is useful for understanding how data is transformed during stream processing.

2. Intermediate State Examination

It facilitates the examination of elements in their intermediate state, assisting in identifying issues or unexpected behaviors.

3. Debugging Complex Pipeline

It is particularly helpful when dealing with complex stream pipelines, providing a way to inspect data without altering the pipeline's behavior. For example, we can use peek in parallelStream to check whether the parallelStream processes the elements in order or in parallel.

Java
 
List<String> fruits = List.of("apple", "blueberry","grapes","banana");
fruits.parallelStream()
        .peek(fruit -> System.out.println("Processing:"+fruit))
        .map(String :: toUpperCase)
        .map(x -> x + "!")
        .forEach(System.out :: println);

Processing:apple
Processing:grapes
Processing:banana
Processing:blueberry
BANANA!
APPLE!
BLUEBERRY!
GRAPES!


In the example above, by using a peek in between, we can see that all elements are being processed out of order since parallelStream uses several threads for processing.

4. Chaining peek() Operations

We can chain multiple peek() operations to perform multiple actions on the elements. Each peek() operates independently.

Java
 
List<String> fruits = List.of("apple", "blueberry","grapes","banana");

fruits.stream()
     .peek(fruit -> System.out.println("Original: " + fruit))
     .map(String::toUpperCase)
     .peek(fruit -> System.out.println("UpperCase: " + fruit))
     .forEach(System.out::println);


5. Statelessness

The peek() operation is intended to be stateless, meaning it should not modify the state of the elements or the stream. It should only perform a non-interfering action on the elements.

Java
 
// Incorrect usage, peek() modifying the state
List<String> fruits = List.of("apple", "blueberry","grapes","banana");
fruits.parallelStream()
        .peek(fruit -> fruit.toUpperCase())
        .map(x -> x + "!")
        .forEach(System.out :: println);


6. Order Preservation

 The peek() operation does not affect the order of elements in the stream. It preserves the order of elements as they appear in the original stream.

Java
 
List<String> fruits = List.of("apple", "blueberry","grapes","banana");

fruits.stream()
        .peek(fruit -> System.out.println("Processing: " + fruit))
        .forEach(System.out::println);


7. Conditional Peeking

You can use peek() conditionally based on some criteria. This can be achieved by combining it with filter() to control when the peeking action should occur.

Java
 
List<String> fruits = List.of("apple", "blueberry","grapes","banana");

fruits.stream()
        .filter(fruit -> fruit.length() > 5)
        .peek(fruit -> System.out.println("Processing long fruit name: " + fruit))
        .forEach(System.out::println);


8. Avoiding Side Effects

 While peek() allows for side effects, it's important to use it judiciously. Excessive reliance on side effects in functional programming may lead to less predictable and maintainable code.

Understanding these aspects of the peek() method can help you use it effectively in various scenarios, such as debugging, logging, or observing elements during stream processing.

Conclusion

peek is intended for observational actions, such as logging or printing. Use it judiciously to avoid unnecessary overhead, especially in performance-critical scenarios. The peek() method is a useful function for adding visibility into the elements of a stream, and it can be beneficial in scenarios where you want to observe or log the intermediate steps of stream processing.

API Stream processing Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • How to Get Word Document Form Values Using Java
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • Exploring Embeddings API With Java and Spring AI

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!