Class MapExtensions


  • @GwtCompatible
    public class MapExtensions
    extends java.lang.Object
    This is an extension library for maps.
    • Constructor Summary

      Constructors 
      Constructor Description
      MapExtensions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      java.util.Map<K,​V>
      filter​(java.util.Map<K,​V> original, Functions.Function2<? super K,​? super V,​java.lang.Boolean> predicate)
      Returns a filtered live view on top of the original map.
      static <K,​V>
      void
      forEach​(java.util.Map<K,​V> map, Procedures.Procedure2<? super K,​? super V> procedure)
      Applies the given procedure for each key value pair of the given map.
      static <K,​V>
      void
      forEach​(java.util.Map<K,​V> map, Procedures.Procedure3<? super K,​? super V,​? super java.lang.Integer> procedure)
      Applies the given procedure for each key value pair of the given map.
      static <K,​V1,​V2>
      java.util.Map<K,​V2>
      mapValues​(java.util.Map<K,​V1> original, Functions.Function1<? super V1,​? extends V2> transformation)
      Returns a map that performs the given transformation for each value of original when requested.
      static <K,​V>
      void
      operator_add​(java.util.Map<K,​V> outputMap, java.util.Map<? extends K,​? extends V> inputMap)
      Add the given entries of the input map into the output map.
      static <K,​V>
      V
      operator_add​(java.util.Map<K,​V> map, Pair<? extends K,​? extends V> entry)
      Add the given pair into the map.
      static <K,​V>
      java.util.Map<K,​V>
      operator_minus​(java.util.Map<K,​V> map, java.lang.Iterable<?> keys)
      Replies the elements of the given map except the pairs with the given keys.
      static <K,​V>
      java.util.Map<K,​V>
      operator_minus​(java.util.Map<K,​V> left, java.util.Map<? extends K,​? extends V> right)
      Replies the elements of the left map without the pairs in the right map.
      static <K,​V>
      java.util.Map<K,​V>
      operator_minus​(java.util.Map<K,​V> map, K key)
      Replies the elements of the given map except the pair with the given key.
      static <K,​V>
      java.util.Map<K,​V>
      operator_minus​(java.util.Map<K,​V> left, Pair<? extends K,​? extends V> right)
      Remove the given pair from a given map for obtaining a new map.
      static <K,​V>
      java.util.Map<K,​V>
      operator_plus​(java.util.Map<K,​V> left, java.util.Map<? extends K,​? extends V> right)
      Merge the two maps.
      static <K,​V>
      java.util.Map<K,​V>
      operator_plus​(java.util.Map<K,​V> left, Pair<? extends K,​? extends V> right)
      Add the given pair to a given map for obtaining a new map.
      static <K,​V>
      void
      operator_remove​(java.util.Map<K,​V> map, java.lang.Iterable<? super K> keysToRemove)
      Remove pairs with the given keys from the map.
      static <K,​V>
      V
      operator_remove​(java.util.Map<K,​V> map, K key)
      Remove a key from the given map.
      static <K,​V>
      boolean
      operator_remove​(java.util.Map<K,​V> map, Pair<? extends K,​? extends V> entry)
      Remove the given pair into the map.
      static <K,​V>
      java.util.Map<K,​V>
      union​(java.util.Map<? extends K,​? extends V> left, java.util.Map<? extends K,​? extends V> right)
      Merge the given maps.
      • Methods inherited from class java.lang.Object

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

      • MapExtensions

        public MapExtensions()
    • Method Detail

      • forEach

        public static <K,​V> void forEach​(java.util.Map<K,​V> map,
                                               Procedures.Procedure2<? super K,​? super V> procedure)
        Applies the given procedure for each key value pair of the given map.
        Parameters:
        map - the map. May not be null.
        procedure - the procedure. May not be null.
      • forEach

        public static <K,​V> void forEach​(java.util.Map<K,​V> map,
                                               Procedures.Procedure3<? super K,​? super V,​? super java.lang.Integer> procedure)
        Applies the given procedure for each key value pair of the given map. The procedure takes the key, the value and a loop counter. If the counter would overflow, Integer.MAX_VALUE is returned for all subsequent pairs. The first pair is at index zero.
        Parameters:
        map - the map. May not be null.
        procedure - the procedure. May not be null.
      • filter

        public static <K,​V> java.util.Map<K,​V> filter​(java.util.Map<K,​V> original,
                                                                  Functions.Function2<? super K,​? super V,​java.lang.Boolean> predicate)

        Returns a filtered live view on top of the original map. Changes to one affect the other.

        The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the original map are reflected in the result map. The results supports removal if the original map supports removal.

        Parameters:
        original - the original map. May not be null.
        predicate - the predicate. May not be null.
        Returns:
        a filtered view on top of the original map. Never null.
      • operator_add

        public static <K,​V> V operator_add​(java.util.Map<K,​V> map,
                                                 Pair<? extends K,​? extends V> entry)
        Add the given pair into the map.

        If the pair key already exists in the map, its value is replaced by the value in the pair, and the old value in the map is returned.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        map - the map to update.
        entry - the entry (key, value) to add into the map.
        Returns:
        the value previously associated to the key, or null if the key was not present in the map before the addition.
        Since:
        2.15
      • operator_add

        public static <K,​V> void operator_add​(java.util.Map<K,​V> outputMap,
                                                    java.util.Map<? extends K,​? extends V> inputMap)
        Add the given entries of the input map into the output map.

        If a key in the inputMap already exists in the outputMap, its value is replaced in the outputMap by the value from the inputMap.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        outputMap - the map to update.
        inputMap - the entries to add.
        Since:
        2.15
      • operator_plus

        @Pure
        public static <K,​V> java.util.Map<K,​V> operator_plus​(java.util.Map<K,​V> left,
                                                                         Pair<? extends K,​? extends V> right)
        Add the given pair to a given map for obtaining a new map.

        The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

        Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        left - the map to consider.
        right - the entry (key, value) to add into the map.
        Returns:
        an immutable map with the content of the map and with the given entry.
        Throws:
        java.lang.IllegalArgumentException - - when the right operand key exists in the left operand.
        Since:
        2.15
      • operator_plus

        @Pure
        public static <K,​V> java.util.Map<K,​V> operator_plus​(java.util.Map<K,​V> left,
                                                                         java.util.Map<? extends K,​? extends V> right)
        Merge the two maps.

        The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

        If a key exists in the left and right operands, the value in the right operand is preferred.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        left - the left map.
        right - the right map.
        Returns:
        a map with the merged contents from the two maps.
        Throws:
        java.lang.IllegalArgumentException - - when a right operand key exists in the left operand.
        Since:
        2.15
      • operator_remove

        public static <K,​V> V operator_remove​(java.util.Map<K,​V> map,
                                                    K key)
        Remove a key from the given map.
        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        map - the map to update.
        key - the key to remove.
        Returns:
        the removed value, or null if the key was not present in the map.
        Since:
        2.15
      • operator_remove

        public static <K,​V> boolean operator_remove​(java.util.Map<K,​V> map,
                                                          Pair<? extends K,​? extends V> entry)
        Remove the given pair into the map.

        If the given key is inside the map, but is not mapped to the given value, the map will not be changed.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        map - the map to update.
        entry - the entry (key, value) to remove from the map.
        Returns:
        true if the pair was removed.
        Since:
        2.15
      • operator_remove

        public static <K,​V> void operator_remove​(java.util.Map<K,​V> map,
                                                       java.lang.Iterable<? super K> keysToRemove)
        Remove pairs with the given keys from the map.
        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        map - the map to update.
        keysToRemove - the keys of the pairs to remove.
        Since:
        2.15
      • operator_minus

        @Pure
        public static <K,​V> java.util.Map<K,​V> operator_minus​(java.util.Map<K,​V> left,
                                                                          Pair<? extends K,​? extends V> right)
        Remove the given pair from a given map for obtaining a new map.

        If the given key is inside the map, but is not mapped to the given value, the map will not be changed.

        The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        left - the map to consider.
        right - the entry (key, value) to remove from the map.
        Returns:
        an immutable map with the content of the map and with the given entry.
        Throws:
        java.lang.IllegalArgumentException - - when the right operand key exists in the left operand.
        Since:
        2.15
      • operator_minus

        @Pure
        public static <K,​V> java.util.Map<K,​V> operator_minus​(java.util.Map<K,​V> map,
                                                                          K key)
        Replies the elements of the given map except the pair with the given key.

        The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        map - the map to update.
        key - the key to remove.
        Returns:
        the map with the content of the map except the key.
        Since:
        2.15
      • operator_minus

        @Pure
        public static <K,​V> java.util.Map<K,​V> operator_minus​(java.util.Map<K,​V> left,
                                                                          java.util.Map<? extends K,​? extends V> right)
        Replies the elements of the left map without the pairs in the right map. If the pair's values differ from the value within the map, the map entry is not removed.

        The difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.

        Since this method uses HashMap instances internally, the keys of the supplied maps must be well-behaved with respect to Object.equals(java.lang.Object) and Object.hashCode().

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        left - the map to update.
        right - the pairs to remove.
        Returns:
        the map with the content of the left map except the pairs of the right map.
        Since:
        2.15
      • operator_minus

        @Pure
        public static <K,​V> java.util.Map<K,​V> operator_minus​(java.util.Map<K,​V> map,
                                                                          java.lang.Iterable<?> keys)
        Replies the elements of the given map except the pairs with the given keys.

        The replied map is a view on the given map. It means that any change in the original map is reflected to the result of this operation.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        map - the map to update.
        keys - the keys of the pairs to remove.
        Returns:
        the map with the content of the map except the pairs.
        Since:
        2.15
      • union

        @Pure
        public static <K,​V> java.util.Map<K,​V> union​(java.util.Map<? extends K,​? extends V> left,
                                                                 java.util.Map<? extends K,​? extends V> right)
        Merge the given maps.

        The replied map is a view on the given two maps. If a key exists in the two maps, the replied value is the value of the right operand.

        Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.

        The replied map is unmodifiable.

        Type Parameters:
        K - type of the map keys.
        V - type of the map values.
        Parameters:
        left - the left map.
        right - the right map.
        Returns:
        a map with the merged contents from the two maps.
        Since:
        2.15
      • mapValues

        @Pure
        public static <K,​V1,​V2> java.util.Map<K,​V2> mapValues​(java.util.Map<K,​V1> original,
                                                                                Functions.Function1<? super V1,​? extends V2> transformation)

        Returns a map that performs the given transformation for each value of original when requested.

        The mapping is done lazily. That is, subsequent access of the values in the map will repeatedly apply the transformation. Characteristics of the original map, such as iteration order, are left intact. Changes in the original map are reflected in the result map. The results supports removal if the original map supports removal.

        Parameters:
        original - the original map. May not be null.
        transformation - the transformation. May not be null.
        Returns:
        a map with equal keys but transformed values. Never null.
        Since:
        2.4