|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectstructure.Hashtable<K,V>
public class Hashtable<K,V>
Implements a dictionary as a table of hashed key-value pairs. Collisions are resolved through linear probing. Values used as keys in this structure must have a hashcode method that returns the same value when two keys are "equals". Initially, a table of suggested size is allocated. It will be expanded as the load factor (ratio of pairs to entries) grows.
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){
Hashtable dict = new Hashtable();
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);
}
ChainedHashtable| Constructor Summary | |
|---|---|
Hashtable()
Construct a hash table that is initially empty. |
|
Hashtable(int initialCapacity)
Construct a hash table that is capable of holding at least initialCapacity values. |
|
| Method Summary | |
|---|---|
void |
clear()
Remove all key-value pairs from 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()
Determine if table is empty. |
java.util.Iterator<V> |
iterator()
Returns a traversal that traverses over the values of the hashtable. |
java.util.Iterator<K> |
keys()
Get a traversal over the keys of the hashtable. |
Set<K> |
keySet()
|
V |
put(K key,
V value)
Place a key-value pair within the table. |
void |
putAll(Map<K,V> other)
Put all of the values found in another map into this map, overriding previous key-value associations. |
V |
remove(K key)
Remove a key-value pair from the table. |
int |
size()
Return the number of key-value pairs within the table. |
java.lang.String |
toString()
Generate a string representation of the hash table. |
Structure<V> |
values()
|
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface structure.Map |
|---|
equals, hashCode |
| Constructor Detail |
|---|
public Hashtable(int initialCapacity)
initialCapacity - The initial capacity of the hash table.public Hashtable()
| 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 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 void putAll(Map<K,V> other)
putAll in interface Map<K,V>other - is the source mappingpublic V remove(K key)
remove in interface Map<K,V>key - The key of the key-value pair to be removed.
public Set<Association<K,V>> entrySet()
entrySet in interface Map<K,V>public Set<K> keySet()
keySet in interface Map<K,V>public Structure<V> values()
values in interface Map<K,V>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 | |||||||