Class CaseInsensitiveMap<T>

  • Type Parameters:
    T - the type of mapped values
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.String,​T>

    public class CaseInsensitiveMap<T>
    extends java.util.HashMap<java.lang.String,​T>

    A CaseInsensitiveMap is a specialized HashMap with case insensitive keys of type String.

    All keys will be converted with String.toLowerCase(Locale) with Locale.ENGLISH.

    The keys are stored internally in lower case and all requests convert the specified key first and than compare it with the stored key as usual with Object.equals(Object).

    This implementation is not synchronized. If used by different threads concurrently, external synchronization is necessary.

    Author:
    chm, agz
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsKey​(java.lang.Object key)
      T get​(java.lang.Object key)
      T getOrDefault​(java.lang.Object key, T defaultValue)  
      T put​(java.lang.String key, T value)
      void putAll​(java.util.Map<? extends java.lang.String,​? extends T> m)
      T remove​(java.lang.Object key)
      • Methods inherited from class java.util.HashMap

        clear, clone, compute, computeIfAbsent, computeIfPresent, containsValue, entrySet, forEach, isEmpty, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll, size, values
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

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

        equals, hashCode
    • Constructor Detail

      • CaseInsensitiveMap

        public CaseInsensitiveMap()
        Constructs an empty CaseInsensitiveMap with the default initial capacity (16) and the default load factor (0.75).
      • CaseInsensitiveMap

        public CaseInsensitiveMap​(int initialCapacity,
                                  float loadFactor)
        Constructs an empty CaseInsensitiveMap with the specified initial capacity and load factor.
        Parameters:
        initialCapacity - the initial capacity
        loadFactor - the load factor
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is non-positive
      • CaseInsensitiveMap

        public CaseInsensitiveMap​(int initialCapacity)
        Constructs an empty CaseInsensitiveMap with the specified initial capacity and the default load factor (0.75).
        Parameters:
        initialCapacity - the initial capacity.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative.
      • CaseInsensitiveMap

        public CaseInsensitiveMap​(java.util.Map<? extends java.lang.String,​? extends T> map)
        Constructs a new CaseInsensitiveMap with the same mappings as the specified Map. The CaseInsensitiveMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.
        Parameters:
        map - the Map whose mappings are to be placed in this map
        Throws:
        java.lang.NullPointerException - if the specified map is null
    • Method Detail

      • containsKey

        public boolean containsKey​(java.lang.Object key)

        The key is interpreted case-insensitive.

        Specified by:
        containsKey in interface java.util.Map<java.lang.String,​T>
        Overrides:
        containsKey in class java.util.HashMap<java.lang.String,​T>
      • get

        public T get​(java.lang.Object key)

        The key is interpreted case-insensitive.

        Specified by:
        get in interface java.util.Map<java.lang.String,​T>
        Overrides:
        get in class java.util.HashMap<java.lang.String,​T>
      • put

        public T put​(java.lang.String key,
                     T value)

        The key is converted to lower case before the mapping is stored.

        Specified by:
        put in interface java.util.Map<java.lang.String,​T>
        Overrides:
        put in class java.util.HashMap<java.lang.String,​T>
      • remove

        public T remove​(java.lang.Object key)

        The key is interpreted case-insensitive.

        Specified by:
        remove in interface java.util.Map<java.lang.String,​T>
        Overrides:
        remove in class java.util.HashMap<java.lang.String,​T>
      • putAll

        public void putAll​(java.util.Map<? extends java.lang.String,​? extends T> m)

        The keys in the given map are interpreted case-insensitive.

        Specified by:
        putAll in interface java.util.Map<java.lang.String,​T>
        Overrides:
        putAll in class java.util.HashMap<java.lang.String,​T>
      • getOrDefault

        public T getOrDefault​(java.lang.Object key,
                              T defaultValue)
        Specified by:
        getOrDefault in interface java.util.Map<java.lang.String,​T>
        Overrides:
        getOrDefault in class java.util.HashMap<java.lang.String,​T>