© Fu Cheng 2018
Fu ChengExploring Java 9https://doi.org/10.1007/978-1-4842-3330-6_4

4. Collections, Stream, and Optional

Fu Cheng
(1)
Auckland, New Zealand
 
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() ...

Get Exploring Java 9: Build Modularized Applications in Java 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.