Class DefaultMap<K,V>

java.lang.Object
com.ontology2.pidove.util.DefaultMap<K,V>
Type Parameters:
K - key data type
V - value data type
All Implemented Interfaces:
Map<K,V>

public class DefaultMap<K,V> extends Object implements Map<K,V>
Like the defaultdict in Python. If you get() a key that doesn't have a value, it sets() a value for it. Among other things, the DefaultMap is a solution to the multimap problem, thatis, you can write var m = new DefaultMap(ArrayList::new); m.get("one").add(7) m.get("one").add(8) m.get("one") // equals List.of(7,8)
  • Field Details

    • innerMap

      public final Map<K,V> innerMap
    • defaultValue

      public final Supplier<V> defaultValue
  • Constructor Details

    • DefaultMap

      public DefaultMap(Map<K,V> innerMap, Supplier<V> defaultValue)
      Parameters:
      innerMap - the map contained inside
      defaultValue - a supplier that generates default values for the map; this has to be a Supplier because if you want this to be a container like a List you need a new one each time.
    • DefaultMap

      public DefaultMap(Supplier<V> defaultValue)
      Creates a new DefaultMap backed by a new HashMap
      Parameters:
      defaultValue - a supplier that generates default values for the map; this has to be a Supplier because if you want this to be a container like a List you need a new one each time.
  • Method Details

    • 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)
      It's not so clear what this function should return, since even if a key is not contained in the inner map it would be if you got it. That kind of thinking would mean isEmpty() is always false and size() undefined in most cases. If this bothers you than try a different Multimap implementation.
      Specified by:
      containsKey in interface Map<K,V>
      Parameters:
      key - key whose presence in this map is to be tested
      Returns:
      the same thing the inner map would return
    • containsValue

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

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key - the key whose associated value is to be returned; if there is no associated value for the key, it assigns the default value to that key
      Returns:
      the associated value if it exists, otherwise the newly created default value;
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • 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>