Collections.emptyMap() vs new HashMap()

前端 未结 9 600
南旧
南旧 2020-12-22 21:22

What are some of the situations where I can use Collections.emptyMap() ? The Documentation says I can use this method if I want my collection to be immutable. <

9条回答
  •  梦毁少年i
    2020-12-22 22:03

    It can be useful when you have a function that returns an immutable collection and in some situation there is no data to return so instead of returning null you can return emptyMap()

    It make your code easier and prevent NullPointerException

提交回复
热议问题