Java Map: HashTable Example

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

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts