public final class HashedMap<K,V> extends Object implements Map<K,V>
Map
interface, and serves as a zero-garbage replacement for the JRE's HashMap
class.
Beware that this class reuses the returned Map.Entry
objects when iterating on the entrySet() view.
This is perfectly legal, and well-written applications should be aware of this and not hold references to Map.Entry objects
beyond each iteration loop, but some applications may nevertheless fall foul.
If this does cause a problem for badly implemented applications, then they should use HashedMapEntries
instead of this class,
or alternatively, set the JVM system property -Dgrey.hashedmap.noreuse=Y.
The latter will however negate the primary advantage of this class, namely its non-generation of any garbage, as that will
cause it to allocate a new Map.Entry object on each iteration of an entrySet() collection.
FindBugs warns Map implementors not to reuse Map.Entry objects (PZ_DONT_REUSE_ENTRY_OBJECTS_IN_ITERATORS) but concedes it
is permitted, and its recommendation is merely good practice to accomodate poorly coded applications. However, this class
takes the view that it is not willing to compromise its memory efficiency, and believes it is sufficient to alert its users
to possible pitfalls.
This class does return independent Map.Entry nodes in the entrySet() view's toArray() methods, as required by the Java Map spec.
Like the JRE's HashMap
, this class implements all optional Map
operations.
Beware that this class is single-threaded and non-reentrant.
HashedMapEntries
Constructor and Description |
---|
HashedMap() |
HashedMap(int initcap) |
HashedMap(int initcap,
float factor) |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object val) |
Iterator<Map.Entry<K,V>> |
entriesIterator()
This returns a recycled Map.Entry iterator.
Whereas entrySet().iterator() creates a new Iterator object, this method returns a single instance of that iterator type, that is associated with this Map object. This is not a standard Map method, and is obviously unsuitable for multi-threaded use. |
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object key) |
boolean |
isEmpty() |
Set<K> |
keySet() |
Iterator<K> |
keysIterator()
This returns a recycled keys iterator.
Whereas keySet().iterator() creates a new Iterator object, this method returns a single instance of that iterator type, that is associated with this Map object. This is not a standard Map method, and is obviously unsuitable for multi-threaded use. |
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> srcmap) |
V |
remove(Object key) |
int |
size() |
String |
toString() |
int |
trimToSize() |
Collection<V> |
values() |
Iterator<V> |
valuesIterator()
This returns a recycled values iterator.
Whereas values().iterator() creates a new Iterator object, this method returns a single instance of that iterator type, that is associated with this Map object. This is not a standard Map method, and is obviously unsuitable for multi-threaded use. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
public HashedMap()
public HashedMap(int initcap)
public HashedMap(int initcap, float factor)
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
public boolean containsValue(Object val)
containsValue
in interface Map<K,V>
public int trimToSize()
public Iterator<K> keysIterator()
public Iterator<V> valuesIterator()
public Iterator<Map.Entry<K,V>> entriesIterator()
Copyright 2010-2018 Grey Software (Yusef Badri). All Rights Reserved.