Class UnmodifiableMap<K,​V>

  • Type Parameters:
    K - The entry key type.
    V - The entry value type.
    All Implemented Interfaces:
    java.util.Map<K,​V>

    @Immutable
    public final class UnmodifiableMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    Simple unmodifiable map that preserves entry order when iterating. This data structure is essentially the same as an associative array, but not modifiable. Backing lookup mechanism is a simple hash table. Note that the builder for this differs from the guava immutable map simply by accepting values to be overwritten, and differs from the java.util HashMap by being unmodifiable and order preserving, more like an unmodifiable LinkedHashMap. Since the map is not modifiable it is possible to achieve full compatibility with the Map interface with much simpler backing structures and logic. By requiring all values to be non-null, some of the logic can be even simpler.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  UnmodifiableMap.Builder<K,​V>  
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map.Entry<K,​V>[] entries  
      protected int size  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      UnmodifiableMap.Builder<K,​V>
      builder()  
      static <K,​V>
      UnmodifiableMap.Builder<K,​V>
      builder​(int initialCapacity)  
      void clear()  
      boolean containsKey​(java.lang.Object o)  
      boolean containsValue​(java.lang.Object o)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      copyOf​(java.util.Map<K,​V> map)  
      protected static <K,​V>
      java.util.Map.Entry<K,​V>
      entry​(K key, V value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object obj)  
      V get​(java.lang.Object o)  
      int hashCode()  
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      protected java.util.Set<java.util.Map.Entry<K,​V>> makeEntrySet()  
      protected java.util.Set<K> makeKeySet()  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf()  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(K key, V value)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(K k1, V v1, K k2, V v2)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(K k1, V v1, K k2, V v2, K k3, V v3)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(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)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(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)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(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)  
      static <K,​V>
      UnmodifiableMap<K,​V>
      mapOf​(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)  
      UnmodifiableSortedMap<K,​V> orderedBy​(java.util.Comparator<K> comparator)
      Get the map as a sorted map ordered by the specific comparator.
      V put​(K k, V v)  
      void putAll​(java.util.Map<? extends K,​? extends V> map)  
      V remove​(java.lang.Object o)  
      int size()  
      java.lang.String toString()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        clear, compute, computeIfAbsent, computeIfPresent, containsValue, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
    • Field Detail

      • entries

        protected final transient java.util.Map.Entry<K,​V>[] entries
      • size

        protected final transient int size
    • Method Detail

      • copyOf

        @Nonnull
        public static <K,​V> UnmodifiableMap<K,​V> copyOf​(java.util.Map<K,​V> map)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K key,
                                                                   @Nonnull
                                                                   V value)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4,
                                                                   @Nonnull
                                                                   K k5,
                                                                   @Nonnull
                                                                   V v5)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4,
                                                                   @Nonnull
                                                                   K k5,
                                                                   @Nonnull
                                                                   V v5,
                                                                   @Nonnull
                                                                   K k6,
                                                                   @Nonnull
                                                                   V v6)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4,
                                                                   @Nonnull
                                                                   K k5,
                                                                   @Nonnull
                                                                   V v5,
                                                                   @Nonnull
                                                                   K k6,
                                                                   @Nonnull
                                                                   V v6,
                                                                   @Nonnull
                                                                   K k7,
                                                                   @Nonnull
                                                                   V v7)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4,
                                                                   @Nonnull
                                                                   K k5,
                                                                   @Nonnull
                                                                   V v5,
                                                                   @Nonnull
                                                                   K k6,
                                                                   @Nonnull
                                                                   V v6,
                                                                   @Nonnull
                                                                   K k7,
                                                                   @Nonnull
                                                                   V v7,
                                                                   @Nonnull
                                                                   K k8,
                                                                   @Nonnull
                                                                   V v8)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4,
                                                                   @Nonnull
                                                                   K k5,
                                                                   @Nonnull
                                                                   V v5,
                                                                   @Nonnull
                                                                   K k6,
                                                                   @Nonnull
                                                                   V v6,
                                                                   @Nonnull
                                                                   K k7,
                                                                   @Nonnull
                                                                   V v7,
                                                                   @Nonnull
                                                                   K k8,
                                                                   @Nonnull
                                                                   V v8,
                                                                   @Nonnull
                                                                   K k9,
                                                                   @Nonnull
                                                                   V v9)
      • mapOf

        public static <K,​V> UnmodifiableMap<K,​V> mapOf​(@Nonnull
                                                                   K k1,
                                                                   @Nonnull
                                                                   V v1,
                                                                   @Nonnull
                                                                   K k2,
                                                                   @Nonnull
                                                                   V v2,
                                                                   @Nonnull
                                                                   K k3,
                                                                   @Nonnull
                                                                   V v3,
                                                                   @Nonnull
                                                                   K k4,
                                                                   @Nonnull
                                                                   V v4,
                                                                   @Nonnull
                                                                   K k5,
                                                                   @Nonnull
                                                                   V v5,
                                                                   @Nonnull
                                                                   K k6,
                                                                   @Nonnull
                                                                   V v6,
                                                                   @Nonnull
                                                                   K k7,
                                                                   @Nonnull
                                                                   V v7,
                                                                   @Nonnull
                                                                   K k8,
                                                                   @Nonnull
                                                                   V v8,
                                                                   @Nonnull
                                                                   K k9,
                                                                   @Nonnull
                                                                   V v9,
                                                                   @Nonnull
                                                                   K k10,
                                                                   @Nonnull
                                                                   V v10)
      • containsKey

        public boolean containsKey​(java.lang.Object o)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • get

        public V get​(java.lang.Object o)
        Specified by:
        get in interface java.util.Map<K,​V>
      • makeEntrySet

        @Nonnull
        protected java.util.Set<java.util.Map.Entry<K,​V>> makeEntrySet()
      • makeKeySet

        @Nonnull
        protected java.util.Set<K> makeKeySet()
      • orderedBy

        public UnmodifiableSortedMap<K,​V> orderedBy​(@Nonnull
                                                          java.util.Comparator<K> comparator)
        Get the map as a sorted map ordered by the specific comparator.
        Parameters:
        comparator - The comparator to make the map sorted for.
        Returns:
        The sorted map.
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object o)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • keySet

        @Nonnull
        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • values

        @Nonnull
        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
      • entrySet

        @Nonnull
        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
      • put

        public V put​(K k,
                     V v)
        Specified by:
        put in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • putAll

        public void putAll​(@Nonnull
                           java.util.Map<? extends K,​? extends V> map)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • entry

        protected static <K,​V> java.util.Map.Entry<K,​V> entry​(@Nonnull
                                                                          K key,
                                                                          @Nonnull
                                                                          V value)