Searching...
Saturday, 6 October 2012

Java Map: HashTable Example

10:43
HashTabls is Same as HashMap except that its methods are synchronized
The class Hashtable was introduced in JDK 1.0 and uses Enumerations instead of Iterators
 
The Hashtable constructors are shown here:
 
Hashtable( )
Hashtable(int size)
Hashtable(int size, float fillRatio)
Hashtable(Map m)
 

Example

package mycollectiontest;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;

public class CollectionExample {

public static void main(String[] args) {
Map<String, Integer> hashtable = new Hashtable<String, Integer>();
hashtable.put("One", new Integer(1));
hashtable.put("Two", new Integer(2));
hashtable.put("Three", new Integer(3));
Enumeration<String> e = ((Hashtable<String, Integer>) hashtable).keys();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}

}

Output

One
Three
Two

0 comments: