Collections.emptyMap() vs new HashMap()

前端 未结 9 587
南旧
南旧 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.

    0 讨论(0)
  • 2020-12-22 21:59

    From Effective Java, Item #43 - "Return empty arrays or collections, not null" demonstrates returning an empty collection and perhaps even demonstrates using these emptyList(), emptySet(), and emptyMap() methods on the Collections class to get an empty collection that also has the additional benefit of being immutable. From Item #15 "Minimize Mutability".

    From Collections-emptySet-Collections-emptyList-Collections

    Its a type of programming idiom. This is for people that do not want null variables. So before the set gets initialized, they can use the empty set.

    Note: Below code is just an example (change it according to your use case):

    private Set myset = Collections.emptySet();
    
    void initSet() {
       myset = new HashSet();
    }
    void deleteSet() {
       myset = Collections.emptySet();
    }
    

    These methods offer a couple of advantages:

    1. They're more concise because you don't need to explicitly type out the generic type of the collection - it's generally just inferred from the context of the method call.

    2. They're more efficient because they don't bother creating new objects; they just re-use an existing empty and immutable object. This effect is generally very minor, but it's occasionally (well, rarely) important.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-22 22:06

    It is, in my personal experience admittedly, very useful in cases where an API requires a collection of parameters, but you have nothing to provide. For example you may have an API that looks something like this, and does not allow null references:

    public ResultSet executeQuery(String query, Map<String, Object> queryParameters);
    

    If you have a query that doesn't take any parameters, it's certainly a bit wasteful to create a HashMap, which involves allocating an array, when you could just pass in the 'Empty Map' which is effectively a constant, the way it's implemented in java.util.Collections.

    0 讨论(0)
  • 2020-12-22 22:12

    Most of the time we use a constructor to create a new empty map. But the Collections methods offer a couple of advantages to create an empty map using static method java.util.Collections.emptyMap()

    1. They're more concise because you don't need to explicitly type out the generic type of the collection - it's generally just inferred from the context of the method call.

    2. They're more efficient because they don't bother creating new objects; they just re-use an existing empty and immutable object. This effect is generally very minor, but it's occasionally (well, rarely) important.

    0 讨论(0)
提交回复
热议问题