|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.neeve.util.UtlLinkedHashMap<K,V>
public class UtlLinkedHashMap<K,V>
This class represents a Map
collection with real-time
behavior. Unless the map's size exceeds its current capacity,
no dynamic memory allocation is ever performed and response time is
extremely fast and consistent.
UtlLinkedHashMap
has a predictable iteration order, which is the order
in which keys were inserted into the map (similar to
java.util.LinkedHashMap
collection class).
A bi-directional list iterator over the map entries is also
provided
, this iterator can be moved
to the first
or to the
last
entry for unlimited reuse.
Applications may change the resizing policy of UtlLinkedHashMap
by overriding the sizeChanged()
method. For example, to reduce
memory footprint, the map's capacity could be maitained at ±50% of
the current map's size.
This implementation is not synchronized. Multiple threads accessing or modifying the collection must be synchronized externally.
Note: To avoid dynamic memory allocations, UtlLinkedHashMap
maintains an internal pool of Map.Entry
objects. The size
of the pool is determined by the map's capacity. When an entry is
removed from the map, it is automatically restored to the pool.
Modified from: http://www.java2s.com/Code/Java/Collections-Data-Structure/AMapcollectionwithrealtimebehavior.htm
This class IS NOT intended to be used by end users.
Nested Class Summary | |
---|---|
class |
UtlLinkedHashMap.FastIterator
This inner class represents a reusable list iterator over UtlLinkedHashMap entries. |
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Constructor Summary | |
---|---|
UtlLinkedHashMap()
Creates a UtlLinkedHashMap with a capacity of 16 entries. |
|
UtlLinkedHashMap(int capacity)
Creates a UtlLinkedHashMap with the specified capacity. |
|
UtlLinkedHashMap(Map<K,V> map)
Creates a UtlLinkedHashMap , copy of the specified Map . |
Method Summary | |
---|---|
int |
capacity()
Returns the capacity of this UtlLinkedHashMap . |
void |
clear()
Removes all mappings from this UtlLinkedHashMap . |
Object |
clone()
Returns a shallow copy of this UtlLinkedHashMap . |
boolean |
containsKey(Object key)
Indicates if this UtlLinkedHashMap contains a mapping for the specified
key. |
boolean |
containsValue(Object value)
Indicates if this UtlLinkedHashMap maps one or more keys to the
specified value. |
Set<Map.Entry<K,V>> |
entrySet()
Returns a collection view of the mappings contained in this UtlLinkedHashMap . |
boolean |
equals(Object obj)
Compares the specified object with this UtlLinkedHashMap for equality. |
UtlLinkedHashMap.FastIterator |
fastIterator()
Returns a reusable UtlLinkedHashMap.FastIterator over this UtlLinkedHashMap entries
(unique instance per map). |
V |
get(Object key)
Returns the value to which this UtlLinkedHashMap maps the specified key. |
Map.Entry<K,V> |
getEntry(K key)
Returns the entry with the specified key. |
int |
hashCode()
Returns the hash code value for this UtlLinkedHashMap . |
boolean |
isEmpty()
Indicates if this UtlLinkedHashMap contains no key-value mappings. |
Set<K> |
keySet()
Returns a set view of the keys contained in this UtlLinkedHashMap . |
V |
put(K key,
V value)
Associates the specified value with the specified key in this UtlLinkedHashMap . |
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this UtlLinkedHashMap . |
V |
remove(Object key)
Removes the mapping for this key from this UtlLinkedHashMap if present. |
void |
setCapacity(int newCapacity)
Changes the current capacity of this UtlLinkedHashMap . |
int |
size()
Returns the number of key-value mappings in this UtlLinkedHashMap . |
String |
toString()
Returns a String representation of this UtlLinkedHashMap . |
Collection<V> |
values()
Returns a collection view of the values contained in this UtlLinkedHashMap . |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public UtlLinkedHashMap()
UtlLinkedHashMap
with a capacity of 16
entries.
public UtlLinkedHashMap(Map<K,V> map)
UtlLinkedHashMap
, copy of the specified Map
.
If the specified map is not an instance of UtlLinkedHashMap
, the
newly created map has a capacity set to the specified map's size.
The copy has the same order as the original, regardless of the original
map's implementation:TreeMap dictionary = ...; UtlLinkedHashMap dictionaryLookup = new UtlLinkedHashMap(dictionary);
map
- the map whose mappings are to be placed in this map.public UtlLinkedHashMap(int capacity)
UtlLinkedHashMap
with the specified capacity. Unless the
capacity is exceeded, operations on this map do not allocate entries.
For optimum performance, the capacity should be of the same order
of magnitude or larger than the expected map's size.
capacity
- the number of buckets in the hash table; it also
defines the number of pre-allocated entries.Method Detail |
---|
public int size()
UtlLinkedHashMap
.
size
in interface Map<K,V>
public int capacity()
UtlLinkedHashMap
. The capacity defines
the number of buckets in the hash table, as well as the maximum number
of entries the map may contain without allocating memory.
public boolean isEmpty()
UtlLinkedHashMap
contains no key-value mappings.
isEmpty
in interface Map<K,V>
true
if this map contains no key-value mappings;
false
otherwise.public boolean containsKey(Object key)
UtlLinkedHashMap
contains a mapping for the specified
key.
containsKey
in interface Map<K,V>
key
- the key whose presence in this map is to be tested.
true
if this map contains a mapping for the
specified key; false
otherwise.
NullPointerException
- if the key is null
.public boolean containsValue(Object value)
UtlLinkedHashMap
maps one or more keys to the
specified value.
containsValue
in interface Map<K,V>
value
- the value whose presence in this map is to be tested.
true
if this map maps one or more keys to the
specified value.
NullPointerException
- if the key is null
.public V get(Object key)
UtlLinkedHashMap
maps the specified key.
get
in interface Map<K,V>
key
- the key whose associated value is to be returned.
null
if there is no mapping for the key.
NullPointerException
- if key is null
.public Map.Entry<K,V> getEntry(K key)
key
- the key whose associated entry is to be returned.
null
if none.public V put(K key, V value)
UtlLinkedHashMap
. If the UtlLinkedHashMap
previously contained a mapping
for this key, the old value is replaced.
put
in interface Map<K,V>
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.
null
if there was no mapping for key.
A null
return can also indicate that the map
previously associated null
with the specified key.
NullPointerException
- if the key is null
.public UtlLinkedHashMap.FastIterator fastIterator()
UtlLinkedHashMap.FastIterator
over this UtlLinkedHashMap
entries
(unique instance per map). For example:// Iteration without memory allocation! for (FastIterator i=map.fastIterator().toFirst(); i.hasNext();) { Entry entry = i.nextEntry(); ... }
UtlLinkedHashMap.FastIterator
public void putAll(Map<? extends K,? extends V> map)
UtlLinkedHashMap
.
putAll
in interface Map<K,V>
map
- the mappings to be stored in this map.
NullPointerException
- the specified map is null
, or
the specified map contains null
keys.public V remove(Object key)
UtlLinkedHashMap
if present.
remove
in interface Map<K,V>
key
- the key whose mapping is to be removed from the map.
null
if there was no mapping for key.
A null
return can also indicate that the map
previously associated null
with the specified key.
NullPointerException
- if the key is null
.public void clear()
UtlLinkedHashMap
.
clear
in interface Map<K,V>
public void setCapacity(int newCapacity)
UtlLinkedHashMap
. If the capacity
is increased, new entries are allocated and added to the pool.
If the capacity is decreased, entries from the pool are deallocated
(and are garbage collected eventually). The capacity also determined
the number of buckets for the hash table.
newCapacity
- the new capacity of this map.public Object clone()
UtlLinkedHashMap
. The keys and
the values themselves are not cloned.
clone
in class Object
public boolean equals(Object obj)
UtlLinkedHashMap
for equality.
Returns true
if the given object is also a map and the two
maps represent the same mappings (regardless of collection iteration
order).
equals
in interface Map<K,V>
equals
in class Object
obj
- the object to be compared for equality with this map.
true
if the specified object is equal to this map;
false
otherwise.public int hashCode()
UtlLinkedHashMap
.
hashCode
in interface Map<K,V>
hashCode
in class Object
public String toString()
String
representation of this UtlLinkedHashMap
.
toString
in class Object
this.entrySet().toString();
public Collection<V> values()
UtlLinkedHashMap
. The collection is backed by the map, so changes to
the map are reflected in the collection, and vice-versa.
The collection supports element removal, which removes the corresponding
mapping from this map, via the
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
,
and clear
operations. It does not support the
add
or addAll
operations.
values
in interface Map<K,V>
public Set<Map.Entry<K,V>> entrySet()
UtlLinkedHashMap
. Each element in the returned collection is a
Map.Entry
. The collection is backed by the map,
so changes to the map are reflected in the collection, and vice-versa.
The collection supports element removal, which removes the corresponding
mapping from this map, via the
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
,
and clear
operations. It does not support the
add
or addAll
operations.
entrySet
in interface Map<K,V>
public Set<K> keySet()
UtlLinkedHashMap
.
The set is backed by the map, so changes to the map are reflected
in the set, and vice-versa. The set supports element removal,
which removes the corresponding mapping from this map, via the
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
,
and clear
operations. It does not support the
add
or addAll
operations.
keySet
in interface Map<K,V>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |