Java 9 Collection Factory Methods Example

By | June 27, 2021

In this post, We will talk and learn about Java 9 Collection Factory Methods Example with a demo project

  • Java 9 has added simple ways to create immutable Collections without null values by providing a set of <Collection>.of methods.
  • The created collection should not have null values and should not be modified later else it will throw java.lang.NullPointerException Or  java.lang.UnsupportedOperationException at runtime.
  • Java 9 Collection Framework updated to include static factory methods for List, Set and Map interface. These methods are very useful to create unmodifiable objects of collections with a small number of elements.

Java 9 Map Interface ofEntries() Method Example

  • In Java 9, in addition to static Map.of() methods, Map interface includes one more static factory method Map.ofEntries(Entry<? extends K, ? extends V>… entries). This function is used to create a map of Map.Entry instances.

CollectionFactoryMethodsTest.java

If you run CollectionFactoryMethodsTest.java as Java Application then it will give the below output:

That’s all about Java 9 Collection Factory Methods Example

You May Also Like:

Java 9 Jshell Tutorial
Java 9 private Methods in Interfaces
Java 9 Try with Resource Enhancement
Java 9 @SafeVarargs Annotation
Java 9 Stream API improvements

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.