Collections.emptyMap() vs new HashMap()

前端 未结 9 590
南旧
南旧 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 21:57

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

    For the same reason you'd use Collections.unmodifiableMap() at some point. You want to return a Map instance that throws an exception if the user attempts to modify it. It's just a special case: the empty Map.

提交回复
热议问题