Package groovy.lang
Class MapWithDefault<K,V> 
java.lang.Object
groovy.lang.MapWithDefault<K,V> 
- All Implemented Interfaces:
- Map<K,- V> 
A wrapper for Map which allows a default value to be specified using a closure.
 Normally not instantiated directly but used via the DGM 
withDefault method.- Since:
- 1.7.1
- 
Nested Class Summary
- 
Method SummaryModifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanReturns the value to which the specified key is mapped, or the default value as specified by the initializing closure if this map contains no mapping for the key.inthashCode()booleanisEmpty()keySet()static <K,V> Map<K, V> newInstance(Map<K, V> m, boolean autoGrow, boolean autoShrink, Closure<V> initClosure) Decorates the given Map allowing a default value to be specified.static <K,V> Map<K, V> newInstance(Map<K, V> m, Closure<V> initClosure) Decorates the given Map allowing a default value to be specified.Associates the specified value with the specified key in this map.voidintsize()values()Methods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Mapcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
- 
Method Details- 
newInstanceDecorates the given Map allowing a default value to be specified.- Parameters:
- m- a Map to wrap
- initClosure- the closure which when passed the- keyreturns the default value
- Returns:
- the wrapped Map
 
- 
newInstancepublic static <K,V> Map<K,V> newInstance(Map<K, V> m, boolean autoGrow, boolean autoShrink, Closure<V> initClosure) Decorates the given Map allowing a default value to be specified. Allows the behavior to be configured usingautoGrowandautoShrinkparameters. The value ofautoShrinkdoesn't alter any values in the initial wrapped map, but you can start with an empty map and useputAllif you really need the minimal backing map value.- Parameters:
- m- a Map to wrap
- autoGrow- when true, also mutate the map adding in this value; otherwise, don't mutate the map, just return to calculated value
- autoShrink- when true, ensure the key will be removed if attempting to store the default value using put or putAll
- initClosure- the closure which when passed the- keyreturns the default value
- Returns:
- the wrapped Map
- Since:
- 4.0.1
 
- 
sizepublic int size()
- 
isEmptypublic boolean isEmpty()
- 
containsKey- Specified by:
- containsKeyin interface- Map<K,- V> 
 
- 
containsValue- Specified by:
- containsValuein interface- Map<K,- V> 
 
- 
getReturns the value to which the specified key is mapped, or the default value as specified by the initializing closure if this map contains no mapping for the key. IfautoGrowis true and the initializing closure is called, the map is modified to contain the new key and value so that the calculated value is effectively cached if needed again. Otherwise, the map will be unchanged.
- 
putAssociates the specified value with the specified key in this map. IfautoShrinkis true, the initializing closure is called and if it evaluates to the value being stored, the value will not be stored and indeed any existing value will be removed. This can be useful when trying to keep the memory requirements small for large key sets where only a spare number of entries differ from the default.
- 
remove
- 
putAll
- 
clearpublic void clear()
- 
keySet
- 
values
- 
entrySet
- 
equals
- 
hashCodepublic int hashCode()
 
-