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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.