Java 9 Collections factory method

In this tutorial we will show the new feature of Java 9 collections factory method. This allows developers to create a collection using a static method of() which makes creating a collection a lot more concise and simpler.

Java 9 Collections factory method:

Below is an example of how to create a list using the new factory method:

The next example is showing how to create a Set using the new collections factory method:

The final example will show how to create a Map using the factory method:

If you wish you make a map of 10 entires or less you can use the Map.of() method however, if you need to create a larger map then you will need to use the Map.ofEntires() method.

In conclusion, I hope you found this post useful and give you an insight of the new feature of Java 9.

Similar posts:

  1. Java 8 Stream flatmap example
  2. Java 8 Stream filter example
  3. Java 8 Stream map example

References:

  1. Java 9 new features

 

 

Leave a Reply