|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object structure.AbstractMap<K,V> structure.ChainedHashtable<K,V>
public class ChainedHashtable<K,V>
This class implements a hash table whose collisions are resolved through external chaining. Values used as keys in this structure must have a hashcode method that returns the same value when two keys are "equals". Initially, a hash table of suggested size is allocated.
Example Usage:
To create a hashtable by reading a collection of words and definitions from System.in we could use the following:
public static void main (String[] argv){ ChainedHashtable dict = newChainedHashtable()
; ReadStream r = new ReadStream(); String word, def; System.out.println("Enter a word: "); while(!r.eof()){ word = r.readLine(); System.out.println("Enter a definition: "); def = r.readLine(); dict.put(word,def)
; System.out.println("Enter a word: "); } System.out.println(dict); }
Hashtable
Constructor Summary | |
---|---|
ChainedHashtable()
Constructs a reasonably large hashtable. |
|
ChainedHashtable(int size)
Constructs a hashtable with capacity for at size elements before chaining is absolutely required. |
Method Summary | |
---|---|
void |
clear()
Removes the values from the hashtable. |
boolean |
containsKey(K key)
Returns true iff a specific key appears within the table. |
boolean |
containsValue(V value)
Returns true if a specific value appears within the table. |
Set<Association<K,V>> |
entrySet()
|
V |
get(K key)
Get the value associated with a key. |
boolean |
isEmpty()
Returns true if no elements are stored within the table. |
java.util.Iterator<V> |
iterator()
Returns an iterator that traverses over the values of the hashtable. |
java.util.Iterator<K> |
keys()
Get an iterator over the keys of the hashtable. |
Set<K> |
keySet()
|
V |
put(K key,
V value)
Place a key-value pair within the table. |
V |
remove(K key)
Remove a key-value pair from the table. |
int |
size()
Computes the number of elements stored within the hashtable. |
java.lang.String |
toString()
Generate a string representation of the chained hash table. |
Structure<V> |
values()
|
Methods inherited from class structure.AbstractMap |
---|
hashCode, putAll |
Methods inherited from class java.lang.Object |
---|
equals, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface structure.Map |
---|
equals, hashCode, putAll |
Constructor Detail |
---|
public ChainedHashtable(int size)
size
- The number of entries initially allocated.public ChainedHashtable()
Method Detail |
---|
public void clear()
clear
in interface Map<K,V>
public int size()
size
in interface Map<K,V>
public boolean isEmpty()
isEmpty
in interface Map<K,V>
public boolean containsValue(V value)
containsValue
in interface Map<K,V>
value
- The value sought.
public boolean containsKey(K key)
containsKey
in interface Map<K,V>
key
- The key sought.
public java.util.Iterator<V> iterator()
public Set<K> keySet()
keySet
in interface Map<K,V>
public Set<Association<K,V>> entrySet()
entrySet
in interface Map<K,V>
public Structure<V> values()
values
in interface Map<K,V>
public V get(K key)
get
in interface Map<K,V>
key
- The key used to find the desired value.
public java.util.Iterator<K> keys()
public V put(K key, V value)
put
in interface Map<K,V>
key
- The key to be added to table.value
- The value associated with key.
public V remove(K key)
remove
in interface Map<K,V>
key
- The key of the key-value pair to be removed.
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |