This chapter covers topics related to Java 9 changes in collections, Stream, and Optional.
Factory Methods for Collections
Java 9 adds some convenient methods you can use to create immutable collections.
The List.of() Method
The new static method
List.of()
has
a number of overloads that you can use to create immutable lists from zero or many elements; see Listing 4-1.
List.of();
List.of("Hello", "World");
List.of(1, 2, 3);
Listing 4-1.
Example of List.of()
List.of() doesn’t allow null values. Passing null to it results in NullPointerException
.
The Set.of() Method
The new static method
Set.of() ...