Class MultiLevelMapHelper

java.lang.Object
com.kingsrook.qqq.backend.core.utils.collections.MultiLevelMapHelper

public class MultiLevelMapHelper extends Object
Help you use a multi-level map, such as: Map[String, Map[String, Integer]] countryStateCountMap = new HashMap[](); Where you always want to put new maps at the lower-level if they aren't there, and similarly, you want to start with a 0 for the value under each (final) key. So instead of like: countryStateCountMap.computeIfAbsent("US", () -> new HashMap()); Map stateCountMap = countryStateCountMap.get("US"); stateCountMap.putIfAbsent("MO", 0); stateCountMap.put(stateCountMap.get("MO") + count); You can just do: MultiLevelMapHelper.getOrPutNextLevel(countryStateCountMap, "US", stateMap -> MultiLevelMapHelper.getOrPutAndIncrement(stateMap, "MO", count)); Or for a bigger map, such as: Map[Integer, Map[String, Map[Integer, Map[String, Integer]]]] bigOleMap = new HashMap[](); getOrPutNextLevel(bigOleMap, clientId, map -> getOrPutNextLevel(map, sku, map2 -> getOrPutNextLevel(map2, warehouseId, map3 -> getOrPutAndIncrement(map3, state))));
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K> void
    For the given map, If the key is not found, put a 1 under it.
    static <K> void
    getOrPutAndIncrement(Map<K,Integer> map, K key, Integer amount)
    For the given map, If the key is not found, put the Integer amount under it.
    static <K, V> void
    getOrPutFinalLevel(Map<K,V> map, K key, Supplier<V> notFoundSupplier, Function<V,V> next)
    For the given map, If the key is not found, run the supplier and put its result under that key Then apply the Function next to that value, replacing the value under the key.
    static <K, K2, V> void
    getOrPutNextLevel(Map<K,Map<K2,V>> map, K key, Consumer<Map<K2,V>> next)
    For the given map, If the key is not found, make a new map[1] and put its result under that key Then get the value under that key (the next level map) and pass it to the Consumer next
    static <K, V> void
    getOrPutNextLevel(Map<K,V> map, K key, Supplier<V> notFoundSupplier, Consumer<V> next)
    For the given map, If the key is not found, run the supplier and put its result under that key Then get the value under that key and pass it to the Consumer next

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MultiLevelMapHelper

      public MultiLevelMapHelper()
  • Method Details

    • getOrPutNextLevel

      public static <K, V> void getOrPutNextLevel(Map<K,V> map, K key, Supplier<V> notFoundSupplier, Consumer<V> next)
      For the given map, If the key is not found, run the supplier and put its result under that key Then get the value under that key and pass it to the Consumer next
    • getOrPutNextLevel

      public static <K, K2, V> void getOrPutNextLevel(Map<K,Map<K2,V>> map, K key, Consumer<Map<K2,V>> next)
      For the given map, If the key is not found, make a new map[1] and put its result under that key Then get the value under that key (the next level map) and pass it to the Consumer next. [1] - the new map will be the same type as the input map, if possible - else will be a new HashMap. To control the map type, see the overload that takes a Supplier notFoundSupplier.
    • getOrPutFinalLevel

      public static <K, V> void getOrPutFinalLevel(Map<K,V> map, K key, Supplier<V> notFoundSupplier, Function<V,V> next)
      For the given map, If the key is not found, run the supplier and put its result under that key Then apply the Function next to that value, replacing the value under the key.
    • getOrPutAndIncrement

      public static <K> void getOrPutAndIncrement(Map<K,Integer> map, K key)
      For the given map, If the key is not found, put a 1 under it. else increment the value under the key.
    • getOrPutAndIncrement

      public static <K> void getOrPutAndIncrement(Map<K,Integer> map, K key, Integer amount)
      For the given map, If the key is not found, put the Integer amount under it. else increment the value under the key by the Integer amount.