Java HashTable Example

Java HashTable Example



  • A hash table stores data as (key,value) pair. 
  • The key is any object (usually String)
  • The value is any object - the item being stored


-----------------------------CODE---------------------------------------
import java.util.Hashtable;

class start
{
public static void main(String args[])
{
Hashtable<String,String> employee = new Hashtable<String,String>();

employee.put("One","Mark");
employee.put("One","Paul");
employee.put("Two","James");
employee.put("Three","Sam");
employee.put("Four","John");

for(String x: employee.keySet())
System.out.println(x+" "+employee.get(x));
}
}

-----------------------OUTPUT-----------------------------


One Paul
Three Sam
Four John
Two James
-------------------------------------------------------------

Notice employee One Paul overwrites One Mark, because a set can not have multiple items of the same value.

No comments:

Post a Comment