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.
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.
Opinions expressed by DZone contributors are their own.