public final class HashedMapEntries<K,V> extends Object implements Map<K,V>
Map
interface, and serves as a zero-garbage replacement for the JRE's HashMap
class.
In general, the HashedMap
class should be preferred to this one, as it has a smaller memory footprint, less indirection
and better locality of reference.
However that class reuses objects when iterating on the entrySet() view and while that is perfectly valid, this class provides
a safe option for badly written applications which can't handle that.
Like the JRE's HashMap
, this class implements all optional Map
operations.
Beware that this class is single-threaded and non-reentrant.
Discussion on reuse of Map.Entry nodes:
You should note that the Map.Entry objects returned by this class's iterator are no longer valid once their key has been deleted,
as they will then be nulled and recycled for use by other keys.
This is is not in breach of the required Map interface, but it is at variance with the behaviour of some other Maps such as the JDK's
java.util.HashMap (whose returned Map.Entry objects are dedicated to their key in perpetuity).
Map.Entry
has this to say:
(i) "Map.Entry objects are valid only for the duration of the iteration"
(ii) "the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator,
except through the setValue operation on the map entry"
FindBugs takes both sides, with PZ_DONT_REUSE_ENTRY_OBJECTS_IN_ITERATORS warning Map implementors not to reuse Map.Entry objects
and DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS warning Map users to beware that Map.Entry objects might mutate unexpectedly.
The HashedMapEntries class is therefore in compliance. It reuses Map.Entry objects only if their key has been deleted, and not
under any other circumstances.
It also returns long-lived (ie. will not be recycled) Map.Entry nodes in the entrySet() view's toArray() methods, as required by
the Java Map spec.
HashedMap
Modifier | Constructor and Description |
---|---|
|
HashedMapEntries() |
|
HashedMapEntries(int initcap) |
protected |
HashedMapEntries(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 HashedMapEntries()
public HashedMapEntries(int initcap)
protected HashedMapEntries(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.