4

I was reading some info about iterators, enumeration, etc. So I tested some code to check everything out.

First one example:

List<String> list = new ArrayList<>();
list.add("5");
list.add("1");
list.add("2");
list.add("3");
Iterator<String> iterator = list.iterator();
list.remove("1");
iterator.forEachRemaining(System.out::println);

This code makes the correct and expected output:

Exception in thread "main" java.util.ConcurrentModificationException

But when I tested the same code with only 1 change list.iterator() -> list.stream().iterator()

List<String> list = new ArrayList<>();
list.add("5");
list.add("1");
list.add("2");
list.add("3");
Iterator<String> iterator = list.stream().iterator();
list.remove("1");
iterator.forEachRemaining(System.out::println);

The result was:

5
2
3

My main aim is to understand:

  1. Why is this happens?
  2. What magic makes only one stream() method?
  3. What are the advantages and disadvantages of using stream().iterator?

1 Answer 1

5

When you call stream(), the API internally calls spliterator(), which it similar to iterator().

For ArrayList, the returned Spliterator / Iterator uses an expectedModCount field to compare to the modCount field of the list, to check if the list has been modified.

The implementation of Iterator initializes the expectedModCount field when the iterator is created.

The implementation of Spliterator defers that initialization until the first element is taken from the Spliterator.

Which means that it allows modification of the list between the call to stream() and the call to the terminal operation that actually starts pulling data from the stream.

Sign up to request clarification or add additional context in comments.

1 Comment

These are technical (implementation) details. The more important aspect is that ArrayList’s stream() method returns a late-binding Stream. It also has been specified in the Non-interference section of the package documentation: “For well-behaved stream sources, the source can be modified before the terminal operation commences and those modifications will be reflected in the covered elements.” with example…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.