Class MapUtil


  • public class MapUtil
    extends Object
    utilities for managing Maps
    • Constructor Detail

      • MapUtil

        public MapUtil()
    • Method Detail

      • findOrCreateSet

        public static <K,​T> Set<T> findOrCreateSet​(Map<K,​Set<T>> M,
                                                         K key)
        Parameters:
        M - a mapping from Object -> Set
        key -
        Returns:
        the Set corresponding to key in M; create one if needed
        Throws:
        IllegalArgumentException - if M is null
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • findOrCreateCollection

        public static <K,​T> Collection<T> findOrCreateCollection​(Map<K,​Collection<T>> M,
                                                                       K key)
        Returns:
        the Collection corresponding to key in M; create one if needed
        Throws:
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • findOrCreateList

        public static <K,​T> List<T> findOrCreateList​(Map<K,​List<T>> M,
                                                           K key)
        Returns:
        the Set corresponding to key in M; create one if needed
        Throws:
        IllegalArgumentException - if M is null
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • findOrCreateMap

        public static <K,​K2,​V> Map<K2,​V> findOrCreateMap​(Map<K,​Map<K2,​V>> M,
                                                                           K key)
        Parameters:
        M - a mapping from Object -> Map
        key -
        Returns:
        the Map corresponding to key in M; create one if needed
        Throws:
        IllegalArgumentException - if M is null
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • findOrCreateValue

        public static <K,​V> V findOrCreateValue​(Map<K,​V> M,
                                                      K key,
                                                      Factory<V> factory)
        Throws:
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • findOrCreateWeakHashMap

        public static <K,​V> WeakHashMap<K,​V> findOrCreateWeakHashMap​(Map<Object,​WeakHashMap<K,​V>> M,
                                                                                 Object key)
        Parameters:
        M - a mapping from Object -> WeakHashMap
        key -
        Returns:
        the WeakHashMap corresponding to key in M; create one if needed
        Throws:
        IllegalArgumentException - if M is null
        ClassCastException - if the key is of an inappropriate type for this map (optional)
        NullPointerException - if the specified key is null and this map does not permit null keys (optional)
      • inverseMap

        public static <K,​V> Map<V,​Set<K>> inverseMap​(Map<K,​Set<V>> m)
        Parameters:
        m - a map from key -> Set
        Returns:
        inverted map, value -> Set
        Throws:
        IllegalArgumentException - if m is null
      • invertOneToOneMap

        public static <K,​V> Map<V,​K> invertOneToOneMap​(Map<K,​V> m)
        invert an input map that is one-to-one (i.e., it does not map two different keys to the same value)
        Throws:
        IllegalArgumentException - if m is null
        IllegalArgumentException - if m is not one-to-one
      • groupKeysByValue

        public static <K,​V> Map<Set<K>,​V> groupKeysByValue​(Map<K,​V> m)