Sitemap
Nuwan Zen

Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!

Java9: Collections Factory Methods

Mar 21, 2021

Java 9 comes with new factory methods to create collections easily.

Press enter or click to view image in full size
Photo by Aviv Rachmadian on Unsplash
Set<String> set = Set.of("A","B","C");
System.out.println(set);

List<String> list=List.of("A","B","C");
System.out.println(list);

Map<String,String> map=Map.of("A","Apple","B","Boy","C","Cat");
System.out.println(map);

This of method allows initializing the collection in one go. Without this you will have to go through adding item one by one.

Get Nuwan Zen’s stories in your inbox

Join Medium for free to get updates from this writer.

Read my other blogs:

--

--

Nuwan Zen
Nuwan Zen

Written by Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!