| JGL - The Generic Collection Library for Java |
All Packages Class Hierarchy This Package Previous Next Index
Class com.objectspace.jgl.HashMap
java.lang.Object
|
+----java.util.Dictionary
|
+----com.objectspace.jgl.Map
|
+----com.objectspace.jgl.HashMap
- public class HashMap
- extends Map
A HashMap is an associative container that manages a set of key/value pairs.
A pair is stored in a hashing structure based on the hash code of its key,
which is obtained by using the standard hashCode() function. Keys are
matched by default using a BinaryPredicate that uses equals() for
comparisons. Duplicate keys are not allowed unless explicitly specified.
A HashMap is useful for implementing a collection of one-to-one or
one-to-many mappings.
Insertion can invalidate iterators.
Removal can invalidate iterators.
- See Also:
- BinaryPredicate, HashMap examples
-
HashMap()
- Construct myself to be an empty HashMap that compares key using equals() and
does not allow duplicates.
-
HashMap(BinaryPredicate)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate and does not allow duplicates.
-
HashMap(BinaryPredicate, boolean)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate and conditionally allows duplicates.
-
HashMap(BinaryPredicate, boolean, int, float)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate and conditionally allows duplicates.
-
HashMap(BinaryPredicate, int, float)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate.
-
HashMap(boolean)
- Construct myself to be an empty HashMap that compares keys using equals() and
conditionally allows duplicates.
-
HashMap(HashMap)
- Construct myself to be a shallow copy of an existing HashMap.
-
add(Object)
- Assume that the specified object is a Pair whose first field is a key and whose
second field is a value.
-
add(Object, Object)
- If the key doesn't exist or duplicates are allowed, associate the value with the
key and return null, otherwise don't modify the map and return the current value
associated with the key.
-
allowExpansion(boolean)
- Enable or disable the current expansion mode.
-
allowsDuplicates()
- Return true if I allow duplicate keys.
-
begin()
- Return an iterator positioned at my first pair.
-
clear()
- Remove all of my elements.
-
clone()
- Return a shallow copy of myself.
-
copy(HashMap)
- Become a shallow copy of an existing HashMap.
-
count(Object)
- Return the number of key/value pairs that match a particular key.
-
countValues(Object)
- Return the number of values that match a given object.
-
elements()
- Return an Enumeration to my values.
-
end()
- Return an iterator positioned immediately after my last pair.
-
equalRange(Object)
- Return a range whose first element is an iterator positioned
at the first occurence of a specific key and whose second element is an
iterator positioned immediately after the last occurence of that key.
-
equals(HashMap)
- Return true if I contain exactly the same key/value pairs as another HashMap.
-
equals(Object)
- Return true if I'm equal to another object.
-
expansionAllowed()
- Return true if adding an object to myself could result in an expansion
of the number of hash buckets I currently use.
-
find(Object)
- Find the first key/value pair based on its key and return its position.
-
finish()
- Return an iterator positioned immediately afer my last pair.
-
get(Object)
- Return the value associated with key, or null if the key does not exist.
-
getComparator()
- Return my comparator.
-
getLoadRatio()
- Return my load ratio.
-
hashCode()
- Return my hash code for support of hashing containers
-
isEmpty()
- Return true if I contain no entries.
-
keys()
- Return an Enumeration of all my keys.
-
keys(Object)
- Return an Enumeration of all my keys that are associated with a particular value.
-
lowerBound(Object)
- Return an iterator positioned at the first location that a
pair with a specified key could be inserted without violating the ordering
criteria.
-
maxSize()
- Return the maximum number of entries that I can contain.
-
nextBucketSize(int)
- Return the number of new buckets to create when expanding the
collection.
-
put(Object, Object)
- If the key doesn't exist, associate the value with the key and return null,
otherwise replace the first value associated with the key and return the old value.
-
remove(Enumeration)
- Remove the element at a particular position.
-
remove(Enumeration, Enumeration)
- Remove the elements within a specified range.
-
remove(Object)
- Remove all key/value pairs that match a particular key.
-
remove(Object, int)
- Remove at most a given number of key/value pairs that match a particular key.
-
size()
- Return the number of entries that I contain.
-
start()
- Return an iterator positioned at my first pair.
-
swap(HashMap)
- Swap my contents with another HashMap.
-
toString()
- Return a string that describes me.
-
upperBound(Object)
- Return an iterator positioned at the last location that
a pair with a specified key could be inserted without violating the ordering
criteria.
-
values(Object)
- Return an Enumeration of all my values that are associated with a particular key.
HashMap
public HashMap()
- Construct myself to be an empty HashMap that compares key using equals() and
does not allow duplicates.
HashMap
public HashMap(boolean allowDuplicates)
- Construct myself to be an empty HashMap that compares keys using equals() and
conditionally allows duplicates.
- Parameters:
- allowDuplicates - true if duplicates are allowed.
HashMap
public HashMap(BinaryPredicate comparator)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate and does not allow duplicates.
- Parameters:
- comparator - The predicate for comparing keys.
HashMap
public HashMap(BinaryPredicate comparator,
boolean allowDuplicates)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate and conditionally allows duplicates.
- Parameters:
- comparator - The predicate for comparing keys.
- allowDuplicates - true if duplicates are allowed.
HashMap
public HashMap(BinaryPredicate comparator,
int capacity,
float loadRatio)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate. The initial buckets and load ratio must also be specified.
- Parameters:
- comparator - The predicate for comparing keys.
- capacity - The initial number of hash buckets to reserve.
- loadRatio - The maximum load ratio.
HashMap
public HashMap(BinaryPredicate comparator,
boolean allowDuplicates,
int capacity,
float loadRatio)
- Construct myself to be an empty HashMap that compares keys using the specified
binary predicate and conditionally allows duplicates. The initial buckets and
load ratio must also be specified.
- Parameters:
- comparator - The predicate for comparing keys.
- allowDuplicates - true if duplicates are allowed.
- capacity - The initial number of hash buckets to reserve.
- loadRatio - The maximum load ratio.
HashMap
public HashMap(HashMap map)
- Construct myself to be a shallow copy of an existing HashMap.
- Parameters:
- map - The HashMap to copy.
allowsDuplicates
public boolean allowsDuplicates()
- Return true if I allow duplicate keys.
getComparator
public BinaryPredicate getComparator()
- Return my comparator.
getLoadRatio
public float getLoadRatio()
- Return my load ratio.
clone
public synchronized Object clone()
- Return a shallow copy of myself.
- Overrides:
- clone in class Map
copy
public synchronized void copy(HashMap map)
- Become a shallow copy of an existing HashMap.
- Parameters:
- map - The HashMap that I shall become a shallow copy of.
toString
public synchronized String toString()
- Return a string that describes me.
- Overrides:
- toString in class Map
elements
public synchronized Enumeration elements()
- Return an Enumeration to my values.
- Overrides:
- elements in class Map
start
public ForwardIterator start()
- Return an iterator positioned at my first pair.
- Overrides:
- start in class Map
finish
public ForwardIterator finish()
- Return an iterator positioned immediately afer my last pair.
- Overrides:
- finish in class Map
begin
public synchronized HashMapIterator begin()
- Return an iterator positioned at my first pair.
end
public synchronized HashMapIterator end()
- Return an iterator positioned immediately after my last pair.
isEmpty
public boolean isEmpty()
- Return true if I contain no entries.
- Overrides:
- isEmpty in class Map
size
public int size()
- Return the number of entries that I contain.
- Overrides:
- size in class Map
maxSize
public int maxSize()
- Return the maximum number of entries that I can contain.
- Overrides:
- maxSize in class Map
equals
public boolean equals(Object object)
- Return true if I'm equal to another object.
- Parameters:
- object - The object to compare myself against.
- Overrides:
- equals in class Map
equals
public synchronized boolean equals(HashMap map)
- Return true if I contain exactly the same key/value pairs as another HashMap.
Use equals() to compare values.
- Parameters:
- map - The HashMap to compare myself against.
hashCode
public synchronized int hashCode()
- Return my hash code for support of hashing containers
- Overrides:
- hashCode in class Object
swap
public synchronized void swap(HashMap map)
- Swap my contents with another HashMap.
- Parameters:
- map - The HashMap that I will swap my contents with.
clear
public synchronized void clear()
- Remove all of my elements.
- Overrides:
- clear in class Map
remove
public Object remove(Object key)
- Remove all key/value pairs that match a particular key.
- Parameters:
- key - The key of the pair(s) to be removed.
- Returns:
- the first value pair removed or null if not changed.
- Overrides:
- remove in class Dictionary
remove
public int remove(Object key,
int count)
- Remove at most a given number of key/value pairs that match a particular key.
- Parameters:
- key - The key of the pair(s) to be removed.
- count - The maximum number of the pair(s) to remove.
- Returns:
- Return the number of pairs removed.
remove
public synchronized Object remove(Enumeration e)
- Remove the element at a particular position.
- Parameters:
- e - An Enumeration positioned at the element to remove.
- Returns:
- Return the value associated with the enumeration.
- Throws: IllegalArgumentException
- is the Enumeration isn't a
HashMapIterator for this HashMap object.
remove
public synchronized int remove(Enumeration first,
Enumeration last)
- Remove the elements within a specified range.
- Parameters:
- first - An Enumeration positioned at the first element to remove.
- last - An Enumeration positioned immediately after the last element to remove.
- Returns:
- Return the number of pairs removed.
- Throws: IllegalArgumentException
- is the Enumeration isn't a
HashMapIterator for this HashMap object.
find
public synchronized HashMapIterator find(Object key)
- Find the first key/value pair based on its key and return its position.
If the key is not found, return end().
- Parameters:
- key - The key to locate.
count
public synchronized int count(Object key)
- Return the number of key/value pairs that match a particular key.
- Parameters:
- key - The key to match against.
- Overrides:
- count in class Map
countValues
public synchronized int countValues(Object value)
- Return the number of values that match a given object.
- Parameters:
- value - The value to match against.
- Overrides:
- countValues in class Map
get
public synchronized Object get(Object key)
- Return the value associated with key, or null if the key does not exist.
- Parameters:
- key - The key to search against.
- Overrides:
- get in class Dictionary
put
public synchronized Object put(Object key,
Object value)
- If the key doesn't exist, associate the value with the key and return null,
otherwise replace the first value associated with the key and return the old value.
- Parameters:
- key - The key.
- value - The value.
- Throws: NullPointerException
- If the key or value are equal to null
- Overrides:
- put in class Dictionary
add
public Object add(Object object)
- Assume that the specified object is a Pair whose first field is a key and whose
second field is a value. If the key doesn't exist or duplicates are allowed,
associate the value with the key and return null, otherwise don't modify the map and
return the current value associated with the key.
- Parameters:
- object - The pair to add.
- Throws: IllegalArgumentException
- If the object is not a Pair
- Throws: NullPointerException
- If the object is null or if the first
or second items in the pair are null.
- Overrides:
- add in class Map
add
public synchronized Object add(Object key,
Object value)
- If the key doesn't exist or duplicates are allowed, associate the value with the
key and return null, otherwise don't modify the map and return the current value
associated with the key.
- Parameters:
- key - The key.
- value - The value.
- Throws: NullPointerException
- If the key or value is null.
keys
public synchronized Enumeration keys()
- Return an Enumeration of all my keys.
- Overrides:
- keys in class Dictionary
keys
public synchronized Enumeration keys(Object value)
- Return an Enumeration of all my keys that are associated with a particular value.
- Parameters:
- value - The value to match.
- Overrides:
- keys in class Map
values
public synchronized Enumeration values(Object key)
- Return an Enumeration of all my values that are associated with a particular key.
- Parameters:
- key - The key to match.
- Overrides:
- values in class Map
lowerBound
public synchronized HashMapIterator lowerBound(Object key)
- Return an iterator positioned at the first location that a
pair with a specified key could be inserted without violating the ordering
criteria. If no such location is found, return an iterator positioned at end().
- Parameters:
- key - The key.
upperBound
public synchronized HashMapIterator upperBound(Object key)
- Return an iterator positioned at the last location that
a pair with a specified key could be inserted without violating the ordering
criteria. If no such location is found, return an iterator positioned at end().
- Parameters:
- key - The key.
equalRange
public synchronized Range equalRange(Object key)
- Return a range whose first element is an iterator positioned
at the first occurence of a specific key and whose second element is an
iterator positioned immediately after the last occurence of that key.
Note that all key inbetween these iterators will also match the specified
key. If no matching key is found, both ends of the range will be the
same.
- Parameters:
- object - The key whose bounds are to be found.
expansionAllowed
public boolean expansionAllowed()
- Return true if adding an object to myself could result in an expansion
of the number of hash buckets I currently use.
allowExpansion
public synchronized void allowExpansion(boolean allow)
- Enable or disable the current expansion mode. If disabled, no new
hash buckets will ever be created regardless of my size.
- Parameters:
- allow - The new expansion mode.
nextBucketSize
protected int nextBucketSize(int length)
- Return the number of new buckets to create when expanding the
collection. The number returned must be positive, and should be
greater than the current bucket size. It is advisable to have
the new size be prime (or have few divisors less than 20) for
the best hash distribution.
- Parameters:
- length - The current number of buckets.
- Returns:
- length * 2 + 1
All Packages Class Hierarchy This Package Previous Next Index