Java Collections Static Factory Methods

With Java9 we finally get an easy way to construct collections. I think the map is one that I will be using  quite often. Now I can write:

Map<String,String> lookupMap = Map.ofEntries(
        entry("a", "A"),
        entry("b", "B"),
        entry("c", "C"));

Instead of:

Map<String, String> lookupMap = new HashMap<>();
lookupMap.put("a","A");
lookupMap.put("b","B");
lookupMap.put("c","C");

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.