Class Maps

  • All Implemented Interfaces:
    Utils

    public abstract class Maps
    extends java.lang.Object
    implements Utils
    The utility class for Map for Modern JDK(9+), which supports the feedback if Java Runtime is below JDK 9.
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    MapUtils
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      java.util.Map<K,​V>
      ofMap()
      Returns an unmodifiable empty map.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(java.util.Map.Entry<? extends K,​? extends V>... entries)
      Returns an unmodifiable map containing the provided entries.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1)
      Returns an unmodifiable map containing a single mapping.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2)
      Returns an unmodifiable map containing two mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3)
      Returns an unmodifiable map containing three mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Returns an unmodifiable map containing four mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
      Returns an unmodifiable map containing five mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
      Returns an unmodifiable map containing six mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
      Returns an unmodifiable map containing seven mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
      Returns an unmodifiable map containing eight mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
      Returns an unmodifiable map containing nine mappings.
      static <K,​V>
      java.util.Map<K,​V>
      ofMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
      Returns an unmodifiable map containing ten mappings.
      • Methods inherited from class java.lang.Object

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

      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap()
        Returns an unmodifiable empty map. See Map#of() for details.

        Example Usage

        
             Map<String, Integer> emptyMap = Maps.ofMap();
         
        Type Parameters:
        K - the key type of the empty map
        V - the value type of the empty map
        Returns:
        an empty map that throws UnsupportedOperationException on attempts to modify it
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1)
        Returns an unmodifiable map containing a single mapping.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the mapping's key
        v1 - the mapping's value
        Returns:
        a Map containing the specified mapping
        Throws:
        java.lang.NullPointerException - if the key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2)
        Returns an unmodifiable map containing two mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if the keys are duplicates
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3)
        Returns an unmodifiable map containing three mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4)
        Returns an unmodifiable map containing four mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4,
                                                                 K k5,
                                                                 V v5)
        Returns an unmodifiable map containing five mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        k5 - the fifth mapping's key
        v5 - the fifth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4,
                                                                 K k5,
                                                                 V v5,
                                                                 K k6,
                                                                 V v6)
        Returns an unmodifiable map containing six mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4, "five", 5, "six", 6);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        k5 - the fifth mapping's key
        v5 - the fifth mapping's value
        k6 - the sixth mapping's key
        v6 - the sixth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4,
                                                                 K k5,
                                                                 V v5,
                                                                 K k6,
                                                                 V v6,
                                                                 K k7,
                                                                 V v7)
        Returns an unmodifiable map containing seven mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4,
                                                "five", 5, "six", 6, "seven", 7);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        k5 - the fifth mapping's key
        v5 - the fifth mapping's value
        k6 - the sixth mapping's key
        v6 - the sixth mapping's value
        k7 - the seventh mapping's key
        v7 - the seventh mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4,
                                                                 K k5,
                                                                 V v5,
                                                                 K k6,
                                                                 V v6,
                                                                 K k7,
                                                                 V v7,
                                                                 K k8,
                                                                 V v8)
        Returns an unmodifiable map containing eight mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4,
                                                "five", 5, "six", 6, "seven", 7, "eight", 8);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        k5 - the fifth mapping's key
        v5 - the fifth mapping's value
        k6 - the sixth mapping's key
        v6 - the sixth mapping's value
        k7 - the seventh mapping's key
        v7 - the seventh mapping's value
        k8 - the eighth mapping's key
        v8 - the eighth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4,
                                                                 K k5,
                                                                 V v5,
                                                                 K k6,
                                                                 V v6,
                                                                 K k7,
                                                                 V v7,
                                                                 K k8,
                                                                 V v8,
                                                                 K k9,
                                                                 V v9)
        Returns an unmodifiable map containing nine mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4,
                                                "five", 5, "six", 6, "seven", 7, "eight", 8, "nine", 9);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        k5 - the fifth mapping's key
        v5 - the fifth mapping's value
        k6 - the sixth mapping's key
        v6 - the sixth mapping's value
        k7 - the seventh mapping's key
        v7 - the seventh mapping's value
        k8 - the eighth mapping's key
        v8 - the eighth mapping's value
        k9 - the ninth mapping's key
        v9 - the ninth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3,
                                                                 K k4,
                                                                 V v4,
                                                                 K k5,
                                                                 V v5,
                                                                 K k6,
                                                                 V v6,
                                                                 K k7,
                                                                 V v7,
                                                                 K k8,
                                                                 V v8,
                                                                 K k9,
                                                                 V v9,
                                                                 K k10,
                                                                 V v10)
        Returns an unmodifiable map containing ten mappings.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap("one", 1, "two", 2, "three", 3, "four", 4,
                                                "five", 5, "six", 6, "seven", 7, "eight", 8,
                                                "nine", 9, "ten", 10);
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        k1 - the first mapping's key
        v1 - the first mapping's value
        k2 - the second mapping's key
        v2 - the second mapping's value
        k3 - the third mapping's key
        v3 - the third mapping's value
        k4 - the fourth mapping's key
        v4 - the fourth mapping's value
        k5 - the fifth mapping's key
        v5 - the fifth mapping's value
        k6 - the sixth mapping's key
        v6 - the sixth mapping's value
        k7 - the seventh mapping's key
        v7 - the seventh mapping's value
        k8 - the eighth mapping's key
        v8 - the eighth mapping's value
        k9 - the ninth mapping's key
        v9 - the ninth mapping's value
        k10 - the tenth mapping's key
        v10 - the tenth mapping's value
        Returns:
        a Map containing the specified mappings
        Throws:
        java.lang.IllegalArgumentException - if there are any duplicate keys
        java.lang.NullPointerException - if any key or value is null
      • ofMap

        @Nonnull
        @Immutable
        public static <K,​V> java.util.Map<K,​V> ofMap​(java.util.Map.Entry<? extends K,​? extends V>... entries)
        Returns an unmodifiable map containing the provided entries.

        Example Usage

        
             Map<String, Integer> map = Maps.ofMap(
                 new AbstractMap.SimpleEntry<>("one", 1),
                 new AbstractMap.SimpleEntry<>("two", 2)
             );
         
        Type Parameters:
        K - the Map's key type
        V - the Map's value type
        Parameters:
        entries - the entries to be added to the map
        Returns:
        a Map containing the specified entries
        Throws:
        java.lang.NullPointerException - if any entry or its key/value is null