private final Map q;
public Info()
{
this(Collections.EMPTY_MAP);
}
public Info(final Map q)
{
this.qualifiers = new HashMap(q);
}
public Map getQ()
{
return Collections.unmodifiableMap(q);
}
Do I need to use Collections.unmodifiableMap() because I saw from the JAVA Docs of EMPTY_MAP that it is Immutable?
No. You need to use Collections.unmodifiableMap() if you want to ensure that clients of your class never modify its internal representation – or rather, if you want to ensure that a failure will occur if a client ever tries to do so.
In the case where the internal map
qisCollections.EMPTY_MAPthen you wouldn’t need to wrap it in a call toCollections.unmodifiableMapbecause it’s already unmodifiable. However you have an alternative constructor that createsqas a modifiable Map, and in that case you’d need to protect it before returning it.You can also simply return a copy of
qin which case clients could modify the returned object without modifying your class’s internal state nor raising an exception.