Class Maps

java.lang.Object
com.pnfsoftware.jeb.util.collect.Maps

public class Maps extends Object
Utility methods for maps.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V> List<V>
    addMulti(Map<K,List<V>> map, K key)
    Add a list bucket for the key in the provided multi-map.
    static <K, V, C extends Collection<V>>
    Collection<V>
    collectMulti(Map<K,C> map)
    Collect the values of a multi-map.
    static <K, V extends Comparable<V>>
    LinkedHashMap<K,V>
    createSortedMapByValue(Map<K,V> map, boolean ascending)
    Sort a map by its values.
    static <K, V> V
    get(Map<K,V> map, K key)
    Get a value from a potentially null map.
    static <K, V> V
    get(Map<K,V> map, K key, V safeValue)
    Get a value from a potentially null map.
    static <K, V extends Comparable<V>>
    List<Map.Entry<K,V>>
    Retrieve a list of sorted (key,value) pairs from the provided map, sorted by ascending values.
    static <K, V extends Comparable<V>>
    List<Map.Entry<K,V>>
    getSortedEntriesByValue(Map<K,V> map, boolean descending)
    Retrieve a list of sorted (key,value) pairs from the provided map, sorted by values.
    static <K, V extends Comparable<V>>
    List<V>
    getSortedValues(Map<K,V> map)
    Retrieve the values of a map, sorted by natural ascending.
    static <K, V extends Comparable<V>>
    List<V>
    getSortedValues(Map<K,V> map, boolean ascending)
    Retrieve the values of a map, sorted by natural ascending or descending order.
    static <K, V, C extends Collection<V>>
    boolean
    putMulti(Map<K,C> map, K key, V value, Supplier<C> supplier)
    Add a key-value entry to the provided multi-map.
    static <K, V> List<V>
    putMulti(Map<K,List<V>> map, K key, Collection<V> values)
    Add a collection of key-value entries to the provided multi-map.
    static <K, V> List<V>
    putMulti(Map<K,List<V>> map, K key, V value)
    Add a key-value entry to the provided multi-map.
    static <V> String
    putNoOverwrite(Map<String,V> map, String preferredKey, V value)
    Insert a value to a string-key'ed map with guarantee that no existing key-value pair will be overwritten.
    static <K> boolean
    removeAll(Map<K,?> map, Collection<? extends K> keys)
    Remove all entries whose keys are in the provided collection.
    static <K, V> int
    removeForValue(Map<K,V> map, V v, boolean useReferenceEquality)
    Remove entries that map to the provided value.
    static <K, V, C extends Collection<V>>
    boolean
    removeMulti(Map<K,C> map, K key, V value)
    Remove a key-value stored in a multi-map.
    static <K> boolean
    retainAll(Map<K,?> map, Collection<? extends K> keys)
    Retain only entries whose keys are in the provided collection.
    static <K, V> HashMap<K,V>
    toMap(K key, V value)
    Build a HashMap containing 1 element (key, value).
    static <K, V> Map<K,V>
    toMap(K key, V value, Class<? extends Map> c)
    Build a map containing 1 element (key, value).
    static <K, V> HashMap<K,V>
    toMap(K key, V value, K key2, V value2)
    Build a HashMap containing 2 elements (key, value).
    static <K, V> HashMap<K,V>
    toMap(K key, V value, K key2, V value2, K key3, V value3)
    Build a HashMap containing 3 elements (key, value).

    Methods inherited from class java.lang.Object

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

    • Maps

      public Maps()
  • Method Details

    • get

      public static <K, V> V get(Map<K,V> map, K key)
      Get a value from a potentially null map.
      Parameters:
      map - optional map
      key - key
      Returns:
      the value, null if the map is null or contains a key pointing to a null value
    • get

      public static <K, V> V get(Map<K,V> map, K key, V safeValue)
      Get a value from a potentially null map.
      Parameters:
      map - optional map
      key - key
      safeValue - value returned if the map is null or does not contain the provided key
      Returns:
      the value - note: the returned value can be null if the key is present, but points to a null value
    • toMap

      public static <K, V> HashMap<K,V> toMap(K key, V value)
      Build a HashMap containing 1 element (key, value).
      Parameters:
      key - key
      value - value associated to key
      Returns:
      a new hash map
    • toMap

      public static <K, V> HashMap<K,V> toMap(K key, V value, K key2, V value2)
      Build a HashMap containing 2 elements (key, value).
      Parameters:
      key - key
      value - value associated to key
      key2 - 2nd key
      value2 - value associated to 2nd key
      Returns:
      a new hash map
    • toMap

      public static <K, V> HashMap<K,V> toMap(K key, V value, K key2, V value2, K key3, V value3)
      Build a HashMap containing 3 elements (key, value).
      Parameters:
      key - key
      value - value associated to key
      key2 - 2nd key
      value2 - value associated to 2nd key
      key3 - 3rd key
      value3 - value associated to 3rd key
      Returns:
      a new hash map
    • toMap

      public static <K, V> Map<K,V> toMap(K key, V value, Class<? extends Map> c)
      Build a map containing 1 element (key, value).
      Parameters:
      key - key
      value - value associated to key
      c - optional requested map type; use null to obtain a HashMap
      Returns:
      a new map
    • getSortedValues

      public static <K, V extends Comparable<V>> List<V> getSortedValues(Map<K,V> map, boolean ascending)
      Retrieve the values of a map, sorted by natural ascending or descending order. The values must be Comparable objects.
      Parameters:
      map - a map; it must not contain null values
      ascending - true to sort by ascending order, false to sort by descending order
      Returns:
      a list of sorted values; the list can be modified without impacting the map's structure
    • getSortedValues

      public static <K, V extends Comparable<V>> List<V> getSortedValues(Map<K,V> map)
      Retrieve the values of a map, sorted by natural ascending. The values must be Comparable objects.
      Parameters:
      map - a map; it must not contain null values
      Returns:
      a list of sorted values; the list can be modified without impacting the map's structure
    • createSortedMapByValue

      public static <K, V extends Comparable<V>> LinkedHashMap<K,V> createSortedMapByValue(Map<K,V> map, boolean ascending)
      Sort a map by its values. The values must be Comparable objects.
      Parameters:
      map - a map; it must not contain null values
      ascending - true to sort by ascending order, false to sort by descending order
      Returns:
      a map ordered by its values; this map can be modified without impacting the original map's structure
    • putNoOverwrite

      public static <V> String putNoOverwrite(Map<String,V> map, String preferredKey, V value)
      Insert a value to a string-key'ed map with guarantee that no existing key-value pair will be overwritten.
      Parameters:
      map - target map
      preferredKey - the preferred key for insertion
      value - value to insert
      Returns:
      the actual key that was generated for insertion (may be different than the Preferred if using the preferred would lead to an existing mapping to be overwritten)
    • removeAll

      public static <K> boolean removeAll(Map<K,?> map, Collection<? extends K> keys)
      Remove all entries whose keys are in the provided collection.
      Type Parameters:
      K - key type
      Parameters:
      map - map to modify
      keys - keys to remove
      Returns:
      true if the map was modified
    • retainAll

      public static <K> boolean retainAll(Map<K,?> map, Collection<? extends K> keys)
      Retain only entries whose keys are in the provided collection.
      Type Parameters:
      K - key type
      Parameters:
      map - map to modify
      keys - keys to retain
      Returns:
      true if the map was modified
    • addMulti

      public static <K, V> List<V> addMulti(Map<K,List<V>> map, K key)
      Add a list bucket for the key in the provided multi-map. If no list of values exists for the key, a new ArrayList is created with an initial capacity of one.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      map - multi-map to update
      key - key whose bucket should be created or retrieved
      Returns:
      the list for the provided key
    • putMulti

      public static <K, V> List<V> putMulti(Map<K,List<V>> map, K key, V value)
      Add a key-value entry to the provided multi-map. If no list of values exists for the key, a new ArrayList is created with an initial capacity of one (to accommodate the addition of the value).

      Equivalent to putMulti(Map, Object, Object, Supplier) with supplier being ArrayList::new.

      Type Parameters:
      K - key type
      V - value type
      Parameters:
      map - multi-map to update
      key - key
      value - value to add
      Returns:
      the list for the provided key
    • putMulti

      public static <K, V> List<V> putMulti(Map<K,List<V>> map, K key, Collection<V> values)
      Add a collection of key-value entries to the provided multi-map. If no list of values exists for the key, a new ArrayList is created.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      map - multi-map to update
      key - key
      values - values to add
      Returns:
      the list for the provided key
    • putMulti

      public static <K, V, C extends Collection<V>> boolean putMulti(Map<K,C> map, K key, V value, Supplier<C> supplier)
      Add a key-value entry to the provided multi-map. If no list of values exists for the key, a new list is created using the provided listFactory parameter.
      Type Parameters:
      K - key type
      V - value type
      C - collection-of-value type
      Parameters:
      map - the map
      key - the key
      value - value to be added to the collection mapped for key
      supplier - collection factory if no collection exists and is mapped for the provided key
      Returns:
      true if the collection to which the value was added changed (i.e., same semantic as Collection.add(Object))
    • collectMulti

      public static <K, V, C extends Collection<V>> Collection<V> collectMulti(Map<K,C> map)
      Collect the values of a multi-map.
      Type Parameters:
      K - key type
      V - value type stored in the collection values
      C - collection type, mapped by the keys
      Parameters:
      map - a map of key-collections
      Returns:
      the collection of values stored in the multi-map
    • removeMulti

      public static <K, V, C extends Collection<V>> boolean removeMulti(Map<K,C> map, K key, V value)
      Remove a key-value stored in a multi-map.
      Type Parameters:
      K - key type
      V - value type
      C - collection-of-value type
      Parameters:
      map - a map of key-collections
      key - key
      value - value to remove
      Returns:
      true if a key-value was removed, false otherwise
    • removeForValue

      public static <K, V> int removeForValue(Map<K,V> map, V v, boolean useReferenceEquality)
      Remove entries that map to the provided value.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      map - map
      v - a non-null value
      useReferenceEquality - if true, values are compared using ==; else, they are compared using equals()
      Returns:
      the number of entries removed
    • getSortedEntriesByValue

      public static <K, V extends Comparable<V>> List<Map.Entry<K,V>> getSortedEntriesByValue(Map<K,V> map)
      Retrieve a list of sorted (key,value) pairs from the provided map, sorted by ascending values.
      Type Parameters:
      K - key type
      V - value type, which must implement Comparable
      Parameters:
      map - the input map (preferably not containing null values)
      Returns:
      a list of sorted entries (the entries are backed the provided map: modifying them will modify the map); entries mapping to null values will be last in the list
    • getSortedEntriesByValue

      public static <K, V extends Comparable<V>> List<Map.Entry<K,V>> getSortedEntriesByValue(Map<K,V> map, boolean descending)
      Retrieve a list of sorted (key,value) pairs from the provided map, sorted by values.
      Type Parameters:
      K - key type
      V - value type, which must implement Comparable
      Parameters:
      map - the input map (should not contain null values)
      descending - if true, the list is sorted by descending values instead of ascending values
      Returns:
      a list of sorted entries (the entries are backed the provided map: modifying them will modify the map); entries mapping to null values will be last in the list