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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Enforcing Architecture With ArchUnit in Java
  • The End of “Good Enough Agile”
  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  1. DZone
  2. Data Engineering
  3. Databases
  4. About the Java 8 Stream API Bug

About the Java 8 Stream API Bug

The Java Stream API wasn't working the way it was supposed to. There is a fix, but it's interesting to see what exactly went wrong.

By 
A N M Bazlur Rahman user avatar
A N M Bazlur Rahman
DZone Core CORE ·
Dec. 12, 16 · Opinion
Likes (17)
Comment
Save
Tweet
Share
16.6K Views

Join the DZone community and get the full member experience.

Join For Free

Stream API does not work on a SubList of an ArrayList the way it is supposed to work. It contains a bug.

The Java Stream API supports a lazy evaluation. That means that the intermediate operation will not be applied until there is a terminal operation. Let's see an example.

List<Integer> ints = new ArrayList<>();
ints.add(1);
ints.add(2);
ints.add(3);
ints.add(4);
ints.add(5);
ints.add(6);

Stream stream =
   ints.stream()
      .peek(System.out::println)
      .filter(i -> i % 2 == 0);

If we run the above code, there will be no output. The peek and fitter operations will not be evaluated. They are always lazy. However, if we put terminal operation (i.e., forEach), they would be evaluated.

If this is the case, the following code snippet is also true. 

List<Integer> ints = new ArrayList<>();
ints.add(1);
ints.add(2);
ints.add(3);
ints.add(4);

Stream stream = ints.stream();

ints.add(5);
ints.add(6);

stream.forEach(System.out::println);

We first created a list of integers and added a few integers, then called the stream method and put it in a variable. Again, we have added a few more integers. Since the Stream API is lazy, nothing happens until we call the terminal operation forEach. 

The above example works just fine. Now, let's create a sublist from the lists of integers and add a few more integers.

List<Integer> ints = new ArrayList<>();
ints.add(1);
ints.add(2);
ints.add(3);
ints.add(4);
ints = ints.subList(0, 2);

Stream stream = ints.stream();

ints.add(5);
ints.add(6);

stream.forEach(System.out::println);

Now, if we run the above code snippet, it will throw a ConcurrentModificationException. 

Turns out, this is a Java bug. This was reported and the fix will be available in Java 9. The details of the bug can be found at this link. 

However, this bug is not related to the Java Stream API, but it's in the ArrayList implementation of Spliterator.

In Java 8, a new special kind of interface was added to the java.util package called Spliterator. We know that the Java collection framework has an Iterator interface to traverse the collection. This interface has been updated with a new  spliterator()  method that returns Spliterator. The Spliterator can split the collection, partitioning off some of its elements as another Spliterator. This has been added in an effort to allow parallel processing of different parts of a collection.

The idea is that this Spliterator basically divides the data. Now, we can easily use, fork, or join the framework to parallelize work. This is how the stream API exactly works, which means the stream API is dependent on this Spliterator.

ArrayList has an inner class named SubList, which also implements the method and returns an instance of another inner class ArrayListSpliterator as an implemented class of the Spliterator class. 

This is where the bug was kicked in.

However, this bug is fixed in Java 9. You can read the source code here.

API Java (programming language) Stream (computing)

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
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!