K - the generic type of the keysV - the generic type of the valuespublic class SmallMap<K,V> extends Object implements Map<K,V>, Serializable
Map implementation optimized for cases in which:
equals(Object) method is significantly faster than its hashCode() method
The way this map works is that instead of using a hash table, it will do a brute force search over its five keys. When the
above conditions are met, such approach is faster and consumes significantly less memory than a traditional HashMap.
Notice that the entire concept of this map is predicated on those two conditions. Using it in other situations will have the
opposite effect of actually decreasing performance. Ideal case if that of String keys. Because most keys will be of
different sizes, the equals method will be faster than the hashCode and the gain can be obtained. Notice that if we were to
use Integer instead, then the optimization would not occur as the hashCode of an int is the int itself.
If a seventh element is introduced into the map, then it will internally switch gears and start delegating into a
HashMap. Notice there's a performance penalty in copying the contents of this map into the new delegate.
Best performance can be obtained if:
of() factory methods are used to initialize instances (when possible)copy(Map) and copy() methods are used as the preferred way of copying mapsunmodifiable(Map) method is preferred over Collections.unmodifiableMap(Map)
forSize(int) is the preferred way of initializing a map of a known sizeUnmodifiableMap
NOTE: copy(Map) should be used even if the input Map is not a SmallMap.
This map supports concurrent reads but doesn't support concurrent writes.
| Constructor and Description |
|---|
SmallMap()
Creates a new empty instance
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
SmallMap<K,V> |
copy()
Creates a copy of this map.
|
static <K,V> Map<K,V> |
copy(Map<K,V> map)
Returns a copy of the given
map. |
Set<Map.Entry<K,V>> |
entrySet() |
boolean |
equals(Object o) |
static <K,V> Map<K,V> |
forSize(int size)
Creates a new map optimized to have the given
size. |
V |
get(Object key) |
int |
hashCode() |
boolean |
isEmpty() |
Set<K> |
keySet() |
static <K,V> SmallMap<K,V> |
of(K key,
V value)
Creates a new instance of a single entry described by the given
key and value |
static <K,V> SmallMap<K,V> |
of(K k1,
V v1,
K k2,
V v2)
Creates a new instance of a two entries described by the given keys and values
|
static <K,V> SmallMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3)
Creates a new instance of a three entries described by the given keys and values
|
static <K,V> SmallMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4)
Creates a new instance of a four entries described by the given keys and values
|
static <K,V> SmallMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5)
Creates a new instance of a five entries described by the given keys and values
|
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> map) |
V |
remove(Object key) |
int |
size() |
String |
toString() |
static <K,V> Map<K,V> |
unmodifiable(Map<K,V> map)
Returns an unmodifiable version of the given
map. |
Collection<V> |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic static <K,V> SmallMap<K,V> of(K key, V value)
key and valueK - the generic type of the keysV - the generic type of the valueskey - the keyvalue - the valuepublic static <K,V> SmallMap<K,V> of(K k1, V v1, K k2, V v2)
K - the generic type of the keysV - the generic type of the valuesk1 - the first keyv1 - the first valuek2 - the second keyv2 - the second valuepublic static <K,V> SmallMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
K - the generic type of the keysV - the generic type of the valuesk1 - the first keyv1 - the first valuek2 - the second keyv2 - the second valuek3 - the third keyv3 - the third valuepublic static <K,V> SmallMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
K - the generic type of the keysV - the generic type of the valuesk1 - the first keyv1 - the first valuek2 - the second keyv2 - the second valuek3 - the third keyv3 - the third valuek4 - the fourth keyv4 - the fourth valuepublic static <K,V> SmallMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
K - the generic type of the keysV - the generic type of the valuesk1 - the first keyv1 - the first valuek2 - the second keyv2 - the second valuek3 - the third keyv3 - the third valuek4 - the fourth keyv4 - the fourth valuek5 - the fifth keyv5 - the fifth valuepublic static <K,V> Map<K,V> copy(Map<K,V> map)
map. If the size of the map is lower or equal to five, then the result will be
a new SmallMap. Otherwise, a new HashMap will be created with a matching initial capacity.
This method offers better performance when the map is an instance of this class, as custom code is used to more
efficiently copy the underlying data structures used by this class. Therefore, the
unmodifiable(Map) method should be preferred over other similar alternatives in order to leverage said optimization.
K - the generic type of the keyV - the generic type of the valuemap - the map to be copiedmappublic static <K,V> Map<K,V> forSize(int size)
size. If size is lower or equal than 5, then a SmallMap will
be returned. Otherwise, a HashMap with size as the initial capacity will be produced.K - the generic type of the keyV - the generic type of the valuesize - the map's sizepublic static <K,V> Map<K,V> unmodifiable(Map<K,V> map)
map.K - V - map - public SmallMap<K,V> copy()
this instance.public boolean containsKey(Object key)
containsKey in interface Map<K,V>public boolean containsValue(Object value)
containsValue in interface Map<K,V>public boolean equals(Object o)
Copyright © 2021 MuleSoft, Inc.. All rights reserved.