Collections.emptyMap() vs new HashMap()

前端 未结 9 623
南旧
南旧 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条回答
  •  误落风尘
    2020-12-22 22:01

    Why would I want an immutable empty collection? What is the point?

    For the same reasons why you might want immutable objects. Primarily because you can sleep safe at night in the knowledge that multiple threads can access the same instance of an object and that they will all be seeing the same values. Having no items in a collection is still a valid value, which you would want to maintain.

提交回复
热议问题