What is the use of Map.ofEntries() instead of Map.of()

后端 未结 3 1245
深忆病人
深忆病人 2020-12-05 15:14

From the documentation of Map.java -

The Map.of() and Map.ofEntries() static factory methods provide a convenient way to cre

3条回答
  •  不知归路
    2020-12-05 16:08

    Well it's very simple. Map.of() is not a varargs method. There are only overloaded Map.of() for up to 10 entries. On the other hand, Map.ofEntries() is a varargs method, hence allowing you to specify as many entries as you want.

    They could have just added Map.ofEntries() but since many times you only need just a few entries, they also included the Map.of() versions as convenience methods so that you don't need to wrap each key-valur pair inside an Entry.

提交回复
热议问题