Class ConcurrentHashMapNullSafe<K,V>

java.lang.Object
com.cedarsoftware.util.ConcurrentHashMapNullSafe<K,V>
Type Parameters:
K - The type of keys maintained by this map
V - The type of mapped values
All Implemented Interfaces:
Map<K,V>

public class ConcurrentHashMapNullSafe<K,V> extends Object implements Map<K,V>
ConcurrentHashMapNullSafe is a thread-safe implementation of ConcurrentHashMap that allows null keys and null values by using sentinel objects internally.
Author:
John DeRegnaucourt
Copyright Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Constructor Details

    • ConcurrentHashMapNullSafe

      public ConcurrentHashMapNullSafe()
      Constructs a new, empty map with default initial capacity (16) and load factor (0.75).
    • ConcurrentHashMapNullSafe

      public ConcurrentHashMapNullSafe(int initialCapacity)
      Constructs a new, empty map with the specified initial capacity and default load factor (0.75).
      Parameters:
      initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.
      Throws:
      IllegalArgumentException - if the initial capacity is negative.
    • ConcurrentHashMapNullSafe

      public ConcurrentHashMapNullSafe(int initialCapacity, float loadFactor)
      Constructs a new, empty map with the specified initial capacity and load factor.
      Parameters:
      initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.
      loadFactor - the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.
      Throws:
      IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive
    • ConcurrentHashMapNullSafe

      public ConcurrentHashMapNullSafe(Map<? extends K,? extends V> m)
      Constructs a new map with the same mappings as the specified map.
      Parameters:
      m - the map whose mappings are to be placed in this map
      Throws:
      NullPointerException - if the specified map is null
  • Method Details

    • size

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

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Specified by:
      getOrDefault in interface Map<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface Map<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • compute

      public V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      compute in interface Map<K,V>
    • merge

      public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
      Specified by:
      merge in interface Map<K,V>
    • equals

      public boolean equals(Object o)
      Overrides the equals method to ensure proper comparison between two maps. Two maps are considered equal if they contain the same key-value mappings.
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
      Parameters:
      o - the object to be compared for equality with this map
      Returns:
      true if the specified object is equal to this map
    • hashCode

      public int hashCode()
      Overrides the hashCode method to ensure consistency with equals. The hash code of a map is defined to be the sum of the hash codes of each entry in the map.
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this map
    • toString

      public String toString()
      Overrides the toString method to provide a string representation of the map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space).
      Overrides:
      toString in class Object
      Returns:
      a string representation of this map