Mention the benefits that immutable collections have over Java9's "Convenience Factory Methods for Collections" feature
See original GitHub issueJava 9 has just introduced the “Convenience Factory Methods for Collections” feature. As ImmutableList.of()
and similar methods in Guava, these factory methods create unmodifiable collections and prohibit null elements. The created collections are serializable if all of their elements are serializable.
Please find more details on http://openjdk.java.net/jeps/269.
It’s worth considering to extend the Javadoc of the corresponding Guava classes (ImmutableList
, ImmutableMap
and ImmutableSet
) like it happened in 3afc6e45c39504ece02d14f4a462f65acfa98652 to discourage using the old factory methods in favor of the Java 7 diamond syntax.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Java 9 Convenience Factory Methods for Collections - Baeldung
A quick and practical guide to creating collections and maps using Java 9 factory methods.
Read more >JEP 269: Convenience Factory Methods for Collections
Provide static factory methods on the collection interfaces that will create compact, unmodifiable collection instances.
Read more >What are the benefits of immutable collections in Java 9?
By using these factory methods, we can create unmodifiable list, set and map collection objects to reduce the number of lines of code....
Read more >Java Convenience Factory Methods for Collections
The JDK 9 has added static factory methods like of() onto basic Collection interfaces, to create unmodifiable collection objects.
Read more >Immutable Collections with Factory Methods in Java 9
Learn to create immutable collections such as immutable list, immutable set and immutable map using new factory methods in Java 9.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you all for sharing your points on this. All of them are completely reasonable.
But in that case I am wondering if it could be possible to extend the Javadoc or https://github.com/google/guava/wiki/ImmutableCollectionsExplained Wiki page to highlight why Java 9 folks should still prefer the immutable collections provided by Guava instead of using the new ones in JDK.
What about this appended to the “types, not implementations” section?
Expressing the immutability guarantee directly in the type that your code references is a powerful advantage. Although Java 9 offers certain immutable collection factory methods, like {@code Set.of}, we continue to prefer use of our immutable collection classes for this reason.