Other classes and interfaces

In Java 8, two default methods were added to the java.util.Collection interface:

  •  Stream<E> stream(): Returns a stream of the elements of this collection.
  • Stream<E> parallelStream(): Returns (possibly) a parallel stream of the elements of this collection. We say possibly because the JVM attempts to split the stream into several chunks and process them in parallel (if there are several CPUs) or virtually parallel (using the time-sharing of the CPU). This is not always possible; it depends, in part, on the nature of the requested processing.

It means that all of the collection interfaces that extend this interface, including Set and List, have these methods. Here is an example:

List<Integer> list = List.of(1

Get Introduction to Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.