Java Hashtable example
Chapter:
Collections
Last Updated:
07-11-2017 18:07:31 UTC
Program:
/* ............... START ............... */
import java.util.Hashtable;
import java.util.Map;
public class JavaHashTableExample {
public static void main(String args[]) {
Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
hashTable.put(1,"Peter");
hashTable.put(2,"Jack");
hashTable.put(3, "Harry");
hashTable.put(4,"Sharon");
for (Map.Entry m : hashTable.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
}
}
/* ............... END ............... */
Output
4 Sharon
3 Harry
2 Jack
1 Peter
Notes:
-
Java Hashtable class implements a hashtable, which maps keys to values.
- Hashtable is similar to HashMap except it is synchronized.
- Hashtable contains only unique elements.
- Hashtable is synchronized.
- Syntax : public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
- K: It is the type of keys maintained by this map.
- V: It is the type of mapped values.
Tags
Collection, Java Hashtable, Java