The new factory methods in action

After revisiting the existing methods of collection creation, we can now review and enjoy the related API introduced in Java 9. As in a previous section, we start with the List interface. Here is how simple and consistent the immutable list creation can be using the new List.of() factory method:

List<String> immutableList =   List.of("immutableList: Life",       " is", " is", " good!\n\n"); //, null);//immutableList.set(2, "sad.");    //throws exception//immutableList.add("Well...");    //throws exceptionimmutableList.stream().forEach(System.out::print);

As you can see from the previous code comments, the new factory method does not allow including null as the list value.

The immutableSet creation looks similar to this: ...

Get Java 9 High Performance 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.