Class AbstractDefaultingHashMap<K,​V>

java.lang.Object
java.util.AbstractMap<K,​V>
java.util.HashMap<K,​V>
com.onenetwork.platform.tools.collections.AbstractDefaultingHashMap<K,​V>
Type Parameters:
K - key type
V - value type
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>
Direct Known Subclasses:
DefaultingHashMap

public abstract class AbstractDefaultingHashMap<K,​V>
extends java.util.HashMap<K,​V>
Identical to HashMap, except a call to get(Object) for a nonexistent key will call newDefaultValue(Object) and put() the resulting value, then return that. This makes it possible to have a Map implementation where get() never returns null.
See Also:
Serialized Form
  • Nested Class Summary

    Nested classes/interfaces inherited from class java.util.AbstractMap

    java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>

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

    java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
  • Constructor Summary

    Constructors 
    Constructor Description
    AbstractDefaultingHashMap()  
  • Method Summary

    Modifier and Type Method Description
    V get​(java.lang.Object key)
    If a value exists for the given key, return it.
    protected abstract V newDefaultValue​(java.lang.Object key)
    When get(Object) is called for a nonexistent key, this method will be called to initialize a default value for that key and put() it in the Map.

    Methods inherited from class java.util.HashMap

    clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values

    Methods inherited from class java.util.AbstractMap

    equals, hashCode, toString

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Map

    equals, hashCode
  • Constructor Details

  • Method Details

    • get

      public V get​(java.lang.Object key)
      If a value exists for the given key, return it. Else, call newDefaultValue(Object) to initialize a default value, put that in the Map, and return it. Note that this method assumes that the key passed in will be an instance of key type K.
      Specified by:
      get in interface java.util.Map<K,​V>
      Overrides:
      get in class java.util.HashMap<K,​V>
      See Also:
      HashMap.get(java.lang.Object)
    • newDefaultValue

      protected abstract V newDefaultValue​(java.lang.Object key)
      When get(Object) is called for a nonexistent key, this method will be called to initialize a default value for that key and put() it in the Map.
      Parameters:
      key -
      Returns: