DZone
Performance Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > 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 · Performance Zone · Opinion
Like (17)
Save
Tweet
15.40K 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

  • 12 Modern CSS Techniques For Older CSS Problems
  • 10 Programming Habits a Web Developer Should Embrace
  • Is NoOps the End of DevOps?
  • Spring, IoC Containers, and Static Code: Design Principles

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo