T - the key typeU - the value typepublic class ComparingHashMap<T,U>
extends java.lang.Object
Note since the new string hashing behavior is hidden under sun.misc, this class implements an older hashing code which might be exploited with well crafted strings.
The implementation is not thread safe.
Note that this is not a complete replacement for java.util.HashMap
| Modifier and Type | Class and Description |
|---|---|
protected static class |
ComparingHashMap.Entry
The set entry.
|
| Modifier and Type | Field and Description |
|---|---|
protected Func2<? super T,? super T,java.lang.Boolean> |
comparer
The comparer function.
|
protected int |
count
The current element count.
|
protected ComparingHashMap.Entry[] |
entries
The contained entries.
|
protected double |
loadFactor
The load factor before the table is resized.
|
protected int |
threshold
The load count before the table is resized.
|
| Constructor and Description |
|---|
ComparingHashMap()
Default constructor with capacity 16, load factor of 0.75 and nullsafe equals() comparer.
|
ComparingHashMap(Func2<? super T,? super T,java.lang.Boolean> comparer)
Constructor with capacity 16, load factor of 0.75 and the given comparer.
|
ComparingHashMap(int capacity)
Constructor with the capacity, load factor of 0.75 and nullsafe equals() comparer.
|
ComparingHashMap(int capacity,
double loadFactor,
Func2<? super T,? super T,java.lang.Boolean> comparer)
Initializes the set with the given capacity, load factor
and comparer.
|
ComparingHashMap(int capacity,
Func2<? super T,? super T,java.lang.Boolean> comparer)
Constructor with the given capacity, load factor of 0.75 and the given comparer.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addEntry(int hash,
java.lang.Object item,
int idx,
java.lang.Object value)
Adds a new entry to the set, resizing
the container as necessary.
|
U |
computeIfAbsent(java.lang.Object key,
Func0<? extends U> supplier)
Compute and put a new value into the map if the given key is not present.
|
U |
get(java.lang.Object key)
Retrieves the value associated with the key or null
if not present.
|
boolean |
isEmpty() |
U |
put(java.lang.Object key,
U newValue)
Tries to add the given item to this set.
|
U |
putIfAbsent(java.lang.Object key,
U newValue)
Put a new value into the map if the given key is not present.
|
protected void |
resize(int newSize)
Resize the container and remap all its entries.
|
int |
size() |
java.lang.Iterable<U> |
values() |
protected final Func2<? super T,? super T,java.lang.Boolean> comparer
protected ComparingHashMap.Entry[] entries
protected int count
protected double loadFactor
protected int threshold
public ComparingHashMap()
public ComparingHashMap(int capacity)
capacity - the initial capacity.public ComparingHashMap(@Nonnull
Func2<? super T,? super T,java.lang.Boolean> comparer)
comparer - the comparer function.public ComparingHashMap(int capacity,
@Nonnull
Func2<? super T,? super T,java.lang.Boolean> comparer)
capacity - the initial capacity.comparer - the comparer function.public ComparingHashMap(int capacity,
double loadFactor,
@Nonnull
Func2<? super T,? super T,java.lang.Boolean> comparer)
capacity - the initial capacity, will be rounded up to the next power of
2, minimum 16loadFactor - the load factor where after the underlying table is resizedcomparer - the content comparerpublic U put(java.lang.Object key, U newValue)
key - the keynewValue - the new valuepublic U putIfAbsent(java.lang.Object key, U newValue)
key - the keynewValue - the new value if the key is not presentpublic U computeIfAbsent(java.lang.Object key, Func0<? extends U> supplier)
key - the keysupplier - the new value supplier if the map doesn't contain the keyprotected void addEntry(int hash,
java.lang.Object item,
int idx,
java.lang.Object value)
hash - the hash of the itemitem - the itemidx - the indexvalue - the valueprotected void resize(int newSize)
newSize - the new sizepublic boolean isEmpty()
public int size()
public U get(java.lang.Object key)
key - the key to checkpublic java.lang.Iterable<U> values()