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. <
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.
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:
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.
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.
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.
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
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
.
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()
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.
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.