|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--net.walend.collection.AbstractHashMap
This class is a hash table based implementation of the net.walend.collection.Map interface, based very heavily on the java.util.HashMap class by Josh Bloch and ultimately Arthur van Hoff. Any mistakes I'm sure are my own and not theirs.
This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.
Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Nested Class Summary | |
protected static interface |
AbstractHashMap.Entry
Sublcass this abstract class and override newEntry to use specialized Entries. |
private class |
AbstractHashMap.EntryIterator
|
private class |
AbstractHashMap.HashIterator
General purpose iterator. |
private class |
AbstractHashMap.KeyIterator
|
private class |
AbstractHashMap.SimpleEntry
Entries for the hash map. |
private class |
AbstractHashMap.ValueIterator
|
Field Summary | |
private static int |
DEFAULTCAPACITY
|
private static float |
DEFAULTLOADFACTOR
|
private static java.util.Iterator |
emptyIterator
Special purpose iterator for empty hash maps. |
private Identitor |
ident
The identitor that controls this Map |
private float |
loadFactor
The load factor for the hash table. |
private int |
modCount
The number of times this AbstractHashMap has been structurally modified Structural modifications are those that change the number of mappings in the AbstractHashMap or otherwise modify its internal structure (e.g., rehash). |
private static long |
serialVersionUID
|
private int |
size
The total number of mappings in the hash table. |
private AbstractHashMap.Entry[] |
table
The hash table data. |
private int |
threshold
The table is rehashed when its size exceeds this threshold. |
Fields inherited from interface net.walend.collection.Map |
EMPTY |
Constructor Summary | |
protected |
AbstractHashMap()
Constructs a new, empty map with the DefaultIdentitor, a default capacity and load factor, which is 0.75. |
protected |
AbstractHashMap(Identitor identitor)
Constructs a new, empty map with the specified Identitor, a default capacity and load factor, which is 0.75. |
protected |
AbstractHashMap(Identitor identitor,
int initialCapacity)
Constructs a new, empty map with the specified Identitor, initial capacity and default load factor, which is 0.75. |
protected |
AbstractHashMap(Identitor identitor,
int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified Identitor, initial capacity and the specified load factor. |
protected |
AbstractHashMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity, DefaultIdentitor, and default load factor, which is 0.75. |
protected |
AbstractHashMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with a DefaultIdentitor, the specified initial capacity and the specified load factor. |
protected |
AbstractHashMap(Map t)
Constructs a new map with the same Identitor and mappings as the given map. |
protected |
AbstractHashMap(java.util.Map map)
Constructs a new Map with a DefaultIdentitor and the same mappings as the java.util.Map. |
Method Summary | |
protected int |
capacity()
|
protected void |
clear()
Removes all mappings from this map. |
java.lang.Object |
clone()
Returns a shallow copy of this AbstractHashMap instance: the keys and values themselves are not cloned. |
boolean |
containsAll(Map map)
Returns true if this collection contains all of the Objects in the specified collection. |
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value. |
java.lang.Object |
get(java.lang.Object key)
Returns the value to which this map maps the specified key. |
private AbstractHashMap.Entry |
getEntry(java.lang.Object key)
Returns the entry associated with the specified key in the AbstractHashMap. |
Identitor |
getIdentitor()
Returns the Identitor for this Map. |
java.util.Map |
getJavaMap()
Return a java.util.Collection of these Objects. |
Set |
getKeys()
Returns a set view of the keys contained in this map. |
java.lang.Class |
getPrincipleInterface()
Returns the class's principle interface for state comparisons. |
Collection |
getValues()
Returns a collection view of the values contained in this map. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
java.util.Iterator |
keyIterator()
Returns an iterator over the keys. |
protected float |
loadFactor()
|
protected AbstractHashMap.Entry |
newEntry(int hash,
java.lang.Object key,
java.lang.Object value,
AbstractHashMap.Entry next)
Override this method and subclass Entry to create specialized Entries. |
private java.util.Iterator |
newEntryIterator()
|
protected java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Puts the key,value pair in the map. |
protected void |
putAll(Map map)
Copies all of the mappings from the specified map to this map (optional operation). |
private void |
readObject(java.io.ObjectInputStream s)
Reconstitute the AbstractHashMap instance from a stream (i.e., deserialize it). |
private void |
rehash()
Rehashes the contents of this map into a new AbstractHashMap instance with a larger capacity. |
protected java.lang.Object |
remove(java.lang.Object key)
Removes the key, value pair from this Map. |
(package private) void |
removeEntry(AbstractHashMap.Entry doomed)
Removes the specified entry from this AbstractHashMap (and increments modCount). |
private AbstractHashMap.Entry |
removeEntryForKey(java.lang.Object key)
Removes and returns the entry associated with the specified key in the AbstractHashMap. |
private AbstractHashMap.Entry |
removeMapping(AbstractHashMap.Entry entry)
Returns removed entry, or null if no match. |
boolean |
sameContentsAs(Map map)
Returns true if this Collection's contents are equal to c's. |
boolean |
sameStateAs(HasState victem)
If two HasStates have the same internal state, return true. |
int |
size()
Returns the number of key-value mappings in this map. |
java.lang.String |
toString()
|
java.util.Iterator |
valueIterator()
Returns an iterator over the values. |
private void |
writeObject(java.io.ObjectOutputStream s)
Save the state of the AbstractHashMap instance to a stream (i.e., serialize it). |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private Identitor ident
private transient AbstractHashMap.Entry[] table
private transient int size
private int threshold
private final float loadFactor
private transient int modCount
private static final float DEFAULTLOADFACTOR
private static final int DEFAULTCAPACITY
private static java.util.Iterator emptyIterator
private static final long serialVersionUID
Constructor Detail |
protected AbstractHashMap(Identitor identitor, int initialCapacity, float loadFactor)
identitor
- the Identitor for this MapinitialCapacity
- the initial capacity of the AbstractHashMap.loadFactor
- the load factor of the AbstractHashMapjava.lang.IllegalArgumentException
- if the initial capacity is less
than zero, or if the load factor is nonpositive.protected AbstractHashMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity of the AbstractHashMap.loadFactor
- the load factor of the AbstractHashMapjava.lang.IllegalArgumentException
- if the initial capacity is less
than zero, or if the load factor is nonpositive.protected AbstractHashMap(Identitor identitor, int initialCapacity)
identitor
- the Identitor for this MaoinitialCapacity
- the initial capacity of the AbstractHashMap.java.lang.IllegalArgumentException
- if the initial capacity is less
than zero.protected AbstractHashMap(int initialCapacity)
initialCapacity
- the initial capacity of the AbstractHashMap.java.lang.IllegalArgumentException
- if the initial capacity is less
than zero.protected AbstractHashMap(Identitor identitor)
protected AbstractHashMap()
protected AbstractHashMap(Map t)
t
- the map whose mappings are to be placed in this map.protected AbstractHashMap(java.util.Map map)
Method Detail |
public Identitor getIdentitor()
getIdentitor
in interface Map
public int size()
size
in interface Map
public boolean isEmpty()
isEmpty
in interface Map
public boolean containsKey(java.lang.Object key)
containsKey
in interface Map
key
- key whose presence in this Map is to be tested.java.lang.NullPointerException
- if key is null.public boolean containsValue(java.lang.Object value)
containsValue
in interface Map
value
- value whose presence in this map is to be tested.java.lang.NullPointerException
- if value is null.public java.lang.Object get(java.lang.Object key)
get
in interface Map
key
- key whose associated value is to be returned.java.lang.NullPointerException
- if the key is null.public Set getKeys()
getKeys
in interface Map
public Collection getValues()
getValues
in interface Map
java.lang.UnsupportedOperationException
- for nowpublic java.util.Iterator keyIterator()
keyIterator
in interface Map
public java.util.Iterator valueIterator()
valueIterator
in interface Map
public boolean containsAll(Map map)
containsAll
in interface Map
public boolean sameContentsAs(Map map)
sameContentsAs
in interface Map
public java.util.Map getJavaMap()
getJavaMap
in interface Map
private AbstractHashMap.Entry getEntry(java.lang.Object key)
protected java.lang.Object put(java.lang.Object key, java.lang.Object value)
java.lang.NullPointerException
- if the key or value is null.protected java.lang.Object remove(java.lang.Object key)
java.lang.NullPointerException
- if the key is null.private AbstractHashMap.Entry removeEntryForKey(java.lang.Object key)
void removeEntry(AbstractHashMap.Entry doomed)
java.util.ConcurrentModificationException
- if the entry is not in the Mapprivate AbstractHashMap.Entry removeMapping(AbstractHashMap.Entry entry)
protected void clear()
private void rehash()
public java.lang.Object clone()
clone
in class java.lang.Object
protected AbstractHashMap.Entry newEntry(int hash, java.lang.Object key, java.lang.Object value, AbstractHashMap.Entry next)
private java.util.Iterator newEntryIterator()
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
java.io.IOException
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
protected int capacity()
protected float loadFactor()
protected void putAll(Map map)
This implementation iterates over the specified map's entrySet() collection, and calls this map's put operation once for each entry returned by the iteration.
map
- mappings to be stored in this map.java.lang.UnsupportedOperationException
- if the putAll operation
is not supported by this map.java.lang.ClassCastException
- if the class of a key or value in the
specified map prevents it from being stored in this map.java.lang.IllegalArgumentException
- if some aspect of a key or value in
the specified map prevents it from being stored in this map.java.lang.NullPointerException
- this map does not permit null
keys or values, and the specified key or value is
null.public java.lang.Class getPrincipleInterface()
getPrincipleInterface
in interface HasState
public boolean sameStateAs(HasState victem)
For objects with subobjects, Generally this method should only return true if the internal objects are equal. Implement a contentsHaveSameState() method to determine if the contents have the same state.
sameStateAs
in interface HasState
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |