How can I initialise a static Map?

前端 未结 30 1002
慢半拍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:39

    If you only need to add one value to the map you can use Collections.singletonMap:

    Map map = Collections.singletonMap(key, value)
    

提交回复
热议问题