- package coreJava;
-
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Iterator;
- public class HashtableDemo {
- public static void main(String[] args) {
- Hashtable<String,String> ht = new Hashtable<String,String>();
- for(int i = 0;i<10;i++){
- ht.put("key"+i, "value"+i);
- }
-
- for(Iterator<String> itr = ht.keySet().iterator();itr.hasNext();){
- String key = (String)itr.next();
- String value = (String)ht.get(key);
- System.out.println(key+" (for) "+value);
- }
-
- Iterator<String> itr = ht.keySet().iterator();
- while (itr.hasNext()){
- String str = (String)itr.next();
- System.out.println(str+" (while) "+ht.get(str));
- }
-
- Enumeration<String> e = ht.elements();
- while(e.hasMoreElements()){
- System.out.println(e.nextElement());
- }
-
- Enumeration<String> e2 = ht.keys();
- while(e2.hasMoreElements()){
- String key = e2.nextElement();
- System.out.println(key+" = "+ht.get(key));
- }
- }
-
- }