package com.deu.demo;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class myHashTable {
public static void main(String[] args) {
Hashtable<String ,Integer> hb=new Hashtable<String ,Integer>();
hb.put("score1",90);
hb.put("score2",50);
hb.put("score3",new Integer(60));
hb.put("score4",new Integer(70));
hb.put("score5",new Integer(80));
//System.out.println("...."+hb.get("score1")); //Get the value under the key
//要获取一个数字,可以使用以下代码:
Integer n =hb.get("score3");
if (n != null) {
System.out.println("score3 = " + n);
}
System.out.println("-------------1-------------");
Enumeration e=hb.elements();//value
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
Hashtable<String ,String> hb2=new Hashtable<String ,String>();
hb2.put("one", "1");
hb2.put("two", "2");
hb2.put("three", "3");
System.out.println("---------------2-----------------");
Set set=hb2.entrySet();
Iterator iter= set.iterator();
//key value
while(iter.hasNext()){
// String str=iter.next().toString();
// System.out.println(" " +str);
Map.Entry<String, String> entry=(Map.Entry)iter.next();
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println(key+" "+value);
}
System.out.println("-------------3-------------------");
Enumeration gkeys=hb2.keys(); //key
while(gkeys.hasMoreElements()){
String key=(String)gkeys.nextElement();
System.out.print(key+" ");
//System.out.println(key+" "+hb2.get(key));//key and value
}
}
}