How can I initialise a static Map?

前端 未结 30 1551
慢半拍i
慢半拍i 2020-11-22 08:43

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other m

30条回答
  •  不知归路
    2020-11-22 09:18

    If you want unmodifiable map, finally java 9 added a cool factory method of to Map interface. Similar method is added to Set, List as well.

    Map unmodifiableMap = Map.of("key1", "value1", "key2", "value2");

提交回复
热议问题