Java Map implementation that returns a default value instead of null

前端 未结 6 878
萌比男神i
萌比男神i 2020-12-14 06:27

I have a Map> in my code, where I\'d avoid potential null pointers if the map\'s #get() method returned an empty l

6条回答
  •  粉色の甜心
    2020-12-14 07:31

    Guava has a method to do exactly what you want. It is similar to Argote's answer.

    Map> myMap = ...
    Functions.forMap(myMap, Arrays.asList("default", "value"));
    

提交回复
热议问题