Package groovy.lang

# Class MapWithDefault<K,V>

java.lang.Object
groovy.lang.MapWithDefault<K,V>

All Implemented Interfaces:
  `Map<K,V>`

public final class MapWithDefault<K,V> extends Object implements 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

  ## Nested classes/interfaces inherited from interface java.util.Map

  `Map.Entry<K extends Object,V extends Object>`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clear()`
  `boolean`
  `containsKey(Object key)`
  `boolean`
  `containsValue(Object value)`
  `Set<Map.Entry<K,V>>`
  `entrySet()`
  `boolean`
  `equals(Object obj)`
  `V`
  `get(Object key)`
  Returns 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.
  `int`
  `hashCode()`
  `boolean`
  `isEmpty()`
  `Set<K>`
  `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.
  `V`
  `put(K key, V value)`
  Associates the specified value with the specified key in this map.
  `void`
  `putAll(Map<? extends K,? extends V> m)`
  `V`
  `remove(Object key)`
  `int`
  `size()`
  `Collection<V>`
  `values()`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface java.util.Map

  `compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll`

- ## Method Details

  - ### newInstance

    public 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.
    Parameters:
      `m` - a Map to wrap
      `initClosure` - the closure which when passed the `key` returns the default value
    Returns:
      the wrapped Map

  - ### newInstance

    public 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 using `autoGrow` and `autoShrink` parameters. The value of `autoShrink` doesn't alter any values in the initial wrapped map, but you can start with an empty map and use `putAll` if 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 `key` returns the default value
    Returns:
      the wrapped Map
    Since:
      4.0.1

  - ### size

    public int size()
    Specified by:
      `size` in interface `Map<K,V>`

  - ### isEmpty

    public boolean isEmpty()
    Specified by:
      `isEmpty` in interface `Map<K,V>`

  - ### containsKey

    public boolean containsKey(Object key)
    Specified by:
      `containsKey` in interface `Map<K,V>`

  - ### containsValue

    public boolean containsValue(Object value)
    Specified by:
      `containsValue` in interface `Map<K,V>`

  - ### get

    public V get(Object key)
    Returns 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. If `autoGrow` is 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.
    Specified by:
      `get` in interface `Map<K,V>`

  - ### put

    public V put(K key, V value)
    Associates the specified value with the specified key in this map. If `autoShrink` is 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.
    Specified by:
      `put` in interface `Map<K,V>`
    Returns:
      the previous value associated with `key` if any, otherwise `null`.

  - ### remove

    public V remove(Object key)
    Specified by:
      `remove` in interface `Map<K,V>`

  - ### putAll

    public void putAll(Map<? extends K,? extends V> m)
    Specified by:
      `putAll` in interface `Map<K,V>`

  - ### clear

    public void clear()
    Specified by:
      `clear` in interface `Map<K,V>`

  - ### keySet

    public Set<K> keySet()
    Specified by:
      `keySet` in interface `Map<K,V>`

  - ### values

    public Collection<V> values()
    Specified by:
      `values` in interface `Map<K,V>`

  - ### entrySet

    public Set<Map.Entry<K,V>> entrySet()
    Specified by:
      `entrySet` in interface `Map<K,V>`

  - ### equals

    public boolean equals(Object obj)
    Specified by:
      `equals` in interface `Map<K,V>`
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Specified by:
      `hashCode` in interface `Map<K,V>`
    Overrides:
      `hashCode` in class `Object`