Convenience Factory Methods for Collections
With the introduction of functional programming in Java, the interest in and need for immutable objects increased. The functions passed into the methods may be executed in substantially different contexts than the one they were created in, so the need to decrease the chances of unexpected side effects made the case for immutability stronger. Besides, the Java way of creating an unmodifiable collection was quite verbose anyway, so the issue was addressed in Java 9. Here is an example of the code that creates an immutable collection of the Set
interface in Java 8:
Set<String> set = new HashSet<>(); set.add("Life"); set.add("is"); set.add("good!"); set = Collections.unmodifiableSet(set);
After one does it ...
Get Java: High-Performance Apps with Java 9 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.