What’s new in the Java Collections Framework in Java 9? Java collections remain one of the most used features in Java with array lists being the most popular functionality. In Java 9, the main enhancements are the collections convenience factory methods. The factory methods make it easier to create instances of new collections.
Stuart Marks drills into these new features and explains the history, context, and usage so developers can deeply understand and fully benefit from them. So, what’s new?
There is a new family of static methods named “of”, which has been added to list, sets, map methods. In three examples, you can see how the code is streamlined between Java 8 and 9 for list, set and map interfaces.
In Java 9, the new static factory methods return collection instances that are unmodifiable. What are the benefits of unmodifiable collections?
In the new collection instances, nulls are disallowed. In Java 9, the iteration order is randomized and will apply only to the new collection that you get from set.of and map.of. The existing collections will remain the same.
Your serialized collections like ArrayList in JDK 8 will work in JDK 9. If you create instances of these new collections and serialize them they cannot be deserialized on JDK 8 and earlier releases.