public class IntHashtable
extends java.lang.Object
An instance of Hashtable
has two parameters that
affect its efficiency: its capacity and its load
factor. The load factor should be between 0.0 and 1.0. When
the number of entries in the hashtable exceeds the product of the
load factor and the current capacity, the capacity is increased by
calling the rehash
method. Larger load factors use
memory more efficiently, at the expense of larger expected time
per lookup.
If many entries are to be made into a Hashtable
,
creating it with a sufficiently large capacity may allow the
entries to be inserted more efficiently than letting it perform
automatic rehashing as needed to grow the table.
This example creates a hashtable of numbers. It uses the names of the numbers as keys:
IntHashtable numbers = new IntHashtable(10); numbers.put(1, 1000); numbers.put(2, 2000); numbers.put(3, 3000);
To retrieve a number, use the following code:
int i = numbers.get(2); // "two = " + i;Don't forget to catch the ElementNotFoundException in the methods it is thrown.
Modifier and Type | Class and Description |
---|---|
static class |
IntHashtable.DuplicatedKeyException
Exception thrown when allowDuplicateKeys is set to false.
|
protected static class |
IntHashtable.Entry
Hashtable collision list.
|
Modifier and Type | Field and Description |
---|---|
boolean |
allowDuplicateKeys
Set to false to throw a IntHashtable.DuplicatedKeyException if you add a key that already exists.
|
int |
collisions
Computes the number of collisions for a set of inserts.
|
protected int |
count
The total number of entries in the hash table.
|
protected double |
loadFactor
The load factor for the hashtable.
|
protected IntHashtable.Entry[] |
table
The hash table data.
|
protected int |
threshold
Rehashes the table when count exceeds this threshold.
|
Constructor and Description |
---|
IntHashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity
and default load factor of 0.75f.
|
IntHashtable(int initialCapacity,
double loadFactor)
Constructs a new, empty hashtable with the specified initial
capacity and the specified load factor.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears this hashtable so that it contains no keys.
|
boolean |
exists(int key)
Checks if the value with the specified key is mapped in this hashtable.
|
int |
get(int key)
Returns the value to which the specified key is mapped in this hashtable.
|
int |
get(int key,
int defaultValue)
Returns the value to which the specified key is mapped in this hashtable.
|
int |
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this hashtable.
|
int |
getKey(int pos)
Returns the key at the given position, or throws ArrayIndexOutOfBounds if the given position does not exist.
|
IntVector |
getKeys()
Return an IntVector of the keys in the IntHashtable.
|
IntVector |
getValues()
Return a Vector of the values in the Hashtable.
|
int |
incrementValue(int key,
int amount)
Increments the value of a key by the given amount.
|
int |
put(int key,
int value)
Maps the specified
key to the specified
value in this hashtable. |
int |
put(java.lang.Object key,
int value)
Takes out the hashCode from the given key object and calls put(int,int).
|
protected void |
rehash()
Rehashes the contents of the hashtable into a hashtable with a
larger capacity.
|
int |
remove(int key)
Removes the key (and its corresponding value) from this
hashtable.
|
int |
size()
Returns the number of keys in this hashtable.
|
protected IntHashtable.Entry[] table
protected transient int count
protected int threshold
protected double loadFactor
public int collisions
int max = 0xFFFFFFF; for (int h = 5; ; h++) { IntHashtable ht = new IntHashtable(h); ht.put("nbsp".hashCode(),' '); ht.put("shy".hashCode(),''); ht.put("quot".hashCode(),'"'); ... if (ht.collisions < max) { Vm.debug("h: "+h+" colli: "+ht.collisions); max = ht.collisions; if (max == 0) break; } }
public boolean allowDuplicateKeys
public IntHashtable(int initialCapacity)
initialCapacity
- The number of elements you think the hashtable will end with. The hashtable will grow if necessary, but using
a number near or above the final size can improve performance.public IntHashtable(int initialCapacity, double loadFactor)
initialCapacity
- The number of elements you think the hashtable will end with. The hashtable will grow if necessary, but using
a number near or above the final size can improve performance.loadFactor
- a number between 0.0 and 1.0.public void clear()
public int get(int key) throws ElementNotFoundException
key
- a key in the hashtable.ElementNotFoundException
- When the key was not found.get(int, int)
public int get(java.lang.Object key) throws ElementNotFoundException
key
- an Object who's hashcode is the key in the hashtable.ElementNotFoundException
- When the key was not found.java.lang.NullPointerException
- If the key is nullget(int, int)
public boolean exists(int key)
key
- a key in the hashtable.public IntVector getValues()
public IntVector getKeys()
public int put(java.lang.Object key, int value)
allowDuplicateKeys
to false.put(int, int)
public int put(int key, int value)
key
to the specified
value
in this hashtable.
The value can be retrieved by calling the get
method
with a key that is equal to the original key.
key
- the hashtable key.value
- the value.IntHashtable.DuplicatedKeyException
- if allowDuplicateKeys is set to false and another key is already added.Object.equals(java.lang.Object)
,
allowDuplicateKeys
protected void rehash()
public int remove(int key) throws ElementNotFoundException
key
- the key that needs to be removed.INVALID
if the key did not have a mapping.ElementNotFoundException
- When the key was not found.public int size()
public int get(int key, int defaultValue)
key
- a key in the hashtable.defaultValue
if the key is not mapped to any value in
this hashtable.public int getKey(int pos)
java.lang.ArrayIndexOutOfBoundsException
- If the position is out of rangepublic int incrementValue(int key, int amount)