Decorates another Map returning a default value if the map
does not contain the requested key.
When the
DefaultedMap.get(Object) method is called with a key that does not
exist in the map, this map will return the default value specified in
the constructor/factory. Only the get method is altered, so the
Map.containsKey(Object) can be used to determine if a key really
is in the map or not.
The defaulted value is not added to the map.
Compare this behaviour with
LazyMap , which does add the value
to the map (via a Transformer).
For instance:
Map map = new DefaultedMap("NULL");
Object obj = map.get("Surname");
// obj == "NULL"
After the above code is executed the map is still empty.
Note that DefaultedMap is not synchronized and is not thread-safe.
If you wish to use this map from multiple threads concurrently, you must use
appropriate synchronization. The simplest approach is to wrap this map
using
java.util.Collections.synchronizedMap(Map) . This class may throw
exceptions when accessed by concurrent threads without synchronization.
since: Commons Collections 3.2 version: $Revision: 1.7 $ $Date: 2005-11-21 22:52:57 +0000 (Mon, 21 Nov 2005) $ author: Stephen Colebourne author: Rafael U.C. Afonso See Also: LazyMap |