Prev Next

Java / List and its implementations

Difference between Collection.stream().forEach() and Collection.forEach().

The order of Stream.forEach is random while Iterable.forEach is always executed in the iteration order of the Iterable.

If Iterable.forEach is iterating over a synchronized collection, Iterable.forEach takes the collection's lock once and holds it across all the calls to the action method. The Stream.forEach call uses the collection's spliterator, which does not lock.

The action specified in Stream.forEach is required to be non-interfering while Iterable.forEach is allowed to set values in the underlying ArrayList without problems.

More Related questions...

Show more question and Answers...


Comments & Discussions