|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectstructure5.AbstractMap<K,V>
structure5.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 = new ChainedHashtable();
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. |
Iterator<V> |
iterator()
Returns an iterator that traverses over the values of the hashtable. |
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. |
String |
toString()
Generate a string representation of the chained hash table. |
Structure<V> |
values()
|
| Methods inherited from class structure5.AbstractMap |
|---|
hashCode, putAll |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface structure5.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 Iterator<V> iterator()
iterator in interface Iterable<V>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 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 String toString()
toString in class Object
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||