Class MultiKeyMap<T>

java.lang.Object
org.djutils.multikeymap.MultiKeyMap<T>
Type Parameters:
T - value type

public class MultiKeyMap<T>
extends Object
Store for data indexed by a combination of objects. An important difference with a normal (Linked-)HashMap is that the getValue method has a Supplier argument that will be invoked when no value was stored under the provided combination of keys. The value yielded by the Supplier is then stored under that key combination. If this behavior is not wanted, the user can provide the null value for the Supplier argument. The map can store null values, but on retrieval these are indistinguishable from values that were never set. No key values may be null.

Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Version:
$Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2018
Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
  • Constructor Details

    • MultiKeyMap

      public MultiKeyMap​(Class<?>... keyTypes)
      Construct a new MultiKeyMap.
      Parameters:
      keyTypes - Class<?>...; the types of the keys
  • Method Details

    • get

      public T get​(Supplier<T> supplier, Object... keys)
      Retrieve a value from this MultiKeyMap. Can create a new entry if it does not exist yet.
      Parameters:
      supplier - Supplier<T>; supplier of T in case the leaf does not exist yet. Set this to null to suppress creation of new branches and a new leaf
      keys - Object...; list of key objects
      Returns:
      T; the existing value, or the value that was obtained through the supplier. Returns null if the leaf does not exist and supplier is null
    • get

      public T get​(Object... keys)
      Retrieve a value from this MultiKeyMap.
      Parameters:
      keys - Object...; the key objects
      Returns:
      T; value, or null if no value is stored under the provided key objects
    • getSubMap

      public MultiKeyMap<T> getSubMap​(Object... keys)
      Retrieve a sub map.
      Parameters:
      keys - Object...; the key objects (must be at least one item shorter than the full depth)
      Returns:
      MultiKeyMap<T>; the sub map
    • put

      public T put​(T newValue, Object... keys)
      Put (add or replace) a value in this MultiKeyMap.
      Parameters:
      newValue - T; the new value
      keys - Object...; the key objects
      Returns:
      T; the previous value stored under the key objects, or null if no value was currently stored under the key objects
    • getKeys

      public Set<Object> getKeys​(Object... keys)
      Return set of key objects on this level.
      Parameters:
      keys - Object...; list of key objects (may be empty to select the key set at the top level
      Returns:
      Set; set of key objects on this level. This is not a safe copy; this set reflects subsequent changes in this MultiKeyMap and modifying this set would modify this MultiKeyMap (potentially making it inconsistent).
    • clear

      public Object clear​(Object... keys)
      Clears the mapping for a key combination.
      Parameters:
      keys - Object...; key combination to clear the map for
      Returns:
      Object; object that was previously mapped to the key combination, or null if it was not cached.
    • toString

      public String toString()
      Overrides:
      toString in class Object