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

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts