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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  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.

A N M Bazlur Rahman user avatar by
A N M Bazlur Rahman
CORE ·
Dec. 12, 16 · Opinion
Like (17)
Save
Tweet
Share
15.85K 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.

Popular on DZone

  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Understanding and Solving the AWS Lambda Cold Start Problem
  • Introduction to Automation Testing Strategies for Microservices
  • Custom Validators in Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: