Class MapUtils


  • public final class MapUtils
    extends Object
    Utils class for the common pattern of creating an entry in a map if nothing is associated to the requested key. This is typically useful for weights or counters (Double or Integer), or one-to-many mappings (Lists or Sets).
    Author:
    thibautd
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MapUtils.DefaultFactory<T>
      Helper class, to use as a factory if objects should be instanciated using their parameter-less constructor.
      static interface  MapUtils.Factory<T>  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K> double addToDouble​(K key, Map<K,​Double> map, double initialValue, double toAdd)
      Add a given value to the Double associated with the key in this map, or initialize a new one if no mapping exists yet.
      static <K> double addToInteger​(K key, Map<K,​Integer> map, int initialValue, int toAdd)
      Add a given value to the Integer associated with the key in this map, or initialize a new one if no mapping exists yet.
      static <K,​V>
      Collection<V>
      get​(Iterable<K> keys, Map<K,​V> map)
      Fill in a collection with the values associated with the requested keys
      static <K,​T>
      T
      getArbitraryObject​(K key, Map<K,​T> map, MapUtils.Factory<T> fact)
      Gets the object associated with the key in this map, or create one if no mapping exists yet.
      static <K,​V>
      Collection<V>
      getCollection​(K key, Map<K,​Collection<V>> map)
      Gets the collection associated with the key in this map, or create an empty list if no mapping exists yet.
      static <K> Double getDouble​(K key, Map<K,​Double> map, double initialValue)
      Gets the double associated with the key in this map, or create a new one if no mapping exists yet.
      static <K> Integer getInteger​(K key, Map<K,​Integer> map, int initialValue)
      Gets the Integer associated with the key in this map, or create a new one if no mapping exists yet.
      static <K,​V>
      List<V>
      getList​(K key, Map<K,​List<V>> map)
      Gets the collection associated with the key in this map, or create an empty list if no mapping exists yet.
      static <K,​C,​V>
      Map<C,​V>
      getMap​(K key, Map<K,​Map<C,​V>> map)
      Gets the map associated with the key in this map, or create an empty map if no mapping exists yet.
      static <K,​V>
      Set<V>
      getSet​(K key, Map<K,​Set<V>> map)
      Gets the set associated with the key in this map, or create an empty set if no mapping exists yet.
    • Method Detail

      • getCollection

        public static <K,​V> Collection<V> getCollection​(K key,
                                                              Map<K,​Collection<V>> map)
        Gets the collection associated with the key in this map, or create an empty list if no mapping exists yet.
        Type Parameters:
        K - type of the key
        V - parameter of the collection type
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        Returns:
        the collection (evt. newly) associated with the key
      • getList

        public static <K,​V> List<V> getList​(K key,
                                                  Map<K,​List<V>> map)
        Gets the collection associated with the key in this map, or create an empty list if no mapping exists yet.
        Type Parameters:
        K - type of the key
        V - parameter of the List type
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        Returns:
        the List (evt. newly) associated with the key
      • getSet

        public static <K,​V> Set<V> getSet​(K key,
                                                Map<K,​Set<V>> map)
        Gets the set associated with the key in this map, or create an empty set if no mapping exists yet.
        Type Parameters:
        K - type of the key
        V - parameter of the set type
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        Returns:
        the set (evt. newly) associated with the key
      • getMap

        public static <K,​C,​V> Map<C,​V> getMap​(K key,
                                                                Map<K,​Map<C,​V>> map)
        Gets the map associated with the key in this map, or create an empty map if no mapping exists yet.
        Type Parameters:
        K - type of the in the primary map
        C - type of the key in the secondary map
        V - type of the values in the secondary map
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        Returns:
        the Map (evt. newly) associated with the key
      • getArbitraryObject

        public static <K,​T> T getArbitraryObject​(K key,
                                                       Map<K,​T> map,
                                                       MapUtils.Factory<T> fact)
        Gets the object associated with the key in this map, or create one if no mapping exists yet.
        Type Parameters:
        K - type of the key
        T - parameter of the values type
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        fact - the factory to use to create a default object if none is found
        Returns:
        the Object (evt. newly) associated with the key
      • getDouble

        public static <K> Double getDouble​(K key,
                                           Map<K,​Double> map,
                                           double initialValue)
        Gets the double associated with the key in this map, or create a new one if no mapping exists yet.
        Type Parameters:
        K - type of the key
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        initialValue - the value to which new entries should be initialized
        Returns:
        the value (evt. newly) associated with the key
      • addToDouble

        public static <K> double addToDouble​(K key,
                                             Map<K,​Double> map,
                                             double initialValue,
                                             double toAdd)
        Add a given value to the Double associated with the key in this map, or initialize a new one if no mapping exists yet.
        Type Parameters:
        K - type of the key
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        initialValue - the value to which new entries should be initialized
        toAdd - the value to add to the existing mapped values
        Returns:
        the collection (evt. newly) associated with the key
      • getInteger

        public static <K> Integer getInteger​(K key,
                                             Map<K,​Integer> map,
                                             int initialValue)
        Gets the Integer associated with the key in this map, or create a new one if no mapping exists yet.
        Type Parameters:
        K - type of the key
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        initialValue - the value to which new entries should be initialized
        Returns:
        the value (evt. newly) associated with the key
      • addToInteger

        public static <K> double addToInteger​(K key,
                                              Map<K,​Integer> map,
                                              int initialValue,
                                              int toAdd)
        Add a given value to the Integer associated with the key in this map, or initialize a new one if no mapping exists yet.
        Type Parameters:
        K - type of the key
        Parameters:
        key - the key of the mapping
        map - the map in which to search
        initialValue - the value to which new entries should be initialized
        toAdd - the value to add to the existing mapped values
        Returns:
        the collection (evt. newly) associated with the key
      • get

        public static <K,​V> Collection<V> get​(Iterable<K> keys,
                                                    Map<K,​V> map)
        Fill in a collection with the values associated with the requested keys
        Type Parameters:
        K - type of the key
        V - type of the values
        Parameters:
        keys - the keys to search for. If a key appears several times, its value will be added to the collection the corresponding number of times.
        map - the map in which to search
        Returns:
        a new collection, filled in with the values associated with the keys provided, in iteration order.