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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java
  • Techniques You Should Know as a Kafka Streams Developer

Trending

  • Metrics at a Glance for Production Clusters
  • Data Quality: A Novel Perspective for 2025
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java 8 Streams API: Laziness and Performance Optimization

Java 8 Streams API: Laziness and Performance Optimization

Learn more about the most important property of Java 8 Streams — laziness.

By 
Amit Phaltankar user avatar
Amit Phaltankar
·
Updated Dec. 17, 18 · Presentation
Likes (6)
Comment
Save
Tweet
Share
45.2K Views

Join the DZone community and get the full member experience.

Join For Free

We have had a quick overview of the Java 8 Streams API in our prevous post. We looked into the power and simplicity of the Java 8 Streams API, the Intermediate and the Terminal Operations over the streams, and different ways to build streams (e.g. from collections or numerical ranges, etc.). In continuation of the same discussion, in this post, we will move ahead with the streams and have a look at the most important property of Java 8 Streams — laziness. If you are new to the concept of Java 8 streams, please go back and read Understanding Java 8 Streams API.

Laziness Improves Performance?

This is a really tricky question. If the laziness is utilized in the correct manner, the answer is 'yes.' Consider you are on an online shopping site and you searched for a particular type of a product. Usually, most of the websites will show the matching products immediately and a 'loading more' message at the bottom. Finally, all of the search results will be loaded in parts, as described. The intent behind doing this is to keep the user interested by immediately showing him some of the results. While the user is browsing through the loaded products, the rest of the products are being loaded. This is because the site is delaying the complete loading of the entire product list. Consider, if the site does eager loading or early loading of all of the products, the response time would increase and the user might get distracted to something else.

While you are dealing with bigger data or infinite streams, the laziness is a real boon. When the data is processed, we are not sure how the processed data will be used. The eager processing will always process the entire amount of data at the cost of performance and the client might end up utilizing a very small chunk of it. Or, depending upon some condition, the client may not even need to utilize that data. The lazy processing is based on a 'process-only, on-demand' strategy.

Laziness and Java 8 Streams

The current trends are all about big data, parallel processing, and being in real time. A large number of systems are being re-designed to sustain the future challenges of the consistently growing amount of data and high expectations of the performance and scalability. No wonder, if the processing model of the Java Collections API is being empowered in order to meet the future expectations, the Java 8 Streams API is fully based on the 'process-only, on-demand' strategy and, hence, supports laziness. 

In the Java 8 Streams API, the intermediate operations are lazy and their internal processing model is optimized to make it capable of being processed with a large amount of data and high performance.

The original post can be read at Java 8 Streams API — Laziness and Performance Optimization

API Stream (computing) optimization Java (programming language)

Published at DZone with permission of Amit Phaltankar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java
  • Techniques You Should Know as a Kafka Streams Developer

Partner Resources

×

Comments

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: