Class MapCreate<K,V>

Object
MapCreate<K,V>
Type Parameters:
K - identifier (key)-type
V - value-type

public class MapCreate<K,V> extends Object
A tree map that creates a new item, if it doesn't already exist upon a get operation.

Internally it uses a HashMap for it's implementation, and the K and V types must obey the rules for a HashMap (with valid equals, hashcode etc.)

This structure is not inherently thread-safe.

Author:
Owen Feehan
  • Constructor Details

    • MapCreate

      public MapCreate(Supplier<V> createNewElement)
      Creates without a comparator, using a HashMap internally.
      Parameters:
      createNewElement - called as necessary to create a new element in the tree.
    • MapCreate

      public MapCreate(Supplier<V> createNewElement, Comparator<K> comparator)
      Creates with an explicit comparator, and using a TreeMap internally.
      Parameters:
      createNewElement - called as necessary to create a new element in the tree.
      comparator - used to impose an ordering on elements.
  • Method Details

    • get

      public V get(K key)
      Gets an existing element from the map, returning null if it is absent.
      Parameters:
      key - the key for the map query.
      Returns:
      an element, either retrieved from the map, or null, if none exists in the map.
    • computeIfAbsent

      public V computeIfAbsent(K key)
      Gets an existing element from the map, newly creating and storing the element if it's absent.
      Parameters:
      key - the key for the map query.
      Returns:
      an element, either retrieved from the map, or newly created.
    • remove

      public void remove(K key, V value)
      Removes the entry for the specified key only if it is currently mapped to the specified value.
      Parameters:
      key - the key to remove.
      value - the value to remove.
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      The entries in the map.
      Returns:
      a set view of the entries contained in this map.
    • keySet

      public Set<K> keySet()
      The keys in the map.
      Returns:
      a set view of the keys contained in this map.
    • isEmpty

      public boolean isEmpty()
      Whether the map is empty or not.
      Returns:
      true iff the map is empty.
    • iterateEntries

      public <E extends Exception> void iterateEntries(CheckedBiConsumer<K,V,E> operation) throws E
      Iterate over each entry in the map, and apply an operation.
      Type Parameters:
      E - an exception that may be thrown by {code operation}.
      Parameters:
      operation - the operation applied to each element in the map, passing the key and value as parameters.
      Throws:
      E - if operation throws it.