package com.tuyao.map;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
public class Hashtabledemo
{
public static void main(String args[])
{
/*
* Hashtable是线程安全的
*/
Hashtable<Object, Object> ha=new Hashtable<Object, Object>();
for(int i=1;i<=100;i++)
{
ha.put(i, "123");
}
//ha.remove(3);//删除键和关联的值
//方法一:for循环遍历
/*
for(int i=1;i<=ha.size();i++)
{
System.out.println(ha.get(i));
}
*/
//方法二:entrySet()遍历
/*
for(Entry<Object, Object> entry:ha.entrySet())
{
System.out.println(entry.getKey()+"="+entry.getValue());
}
*/
//方法三:keySet()遍历
/*
for(Object obj:ha.keySet())
{
Object value = ha.get(obj);
System.out.println(value);
}
*/
// 方法四:构造器遍历
/*
for(Iterator<Object> itr = ha.keySet().iterator(); itr.hasNext();)
{
int key = (int) itr.next();
String value = (String) ha.get(key);
System.out.println(key+"--"+value);
}
*/
// 方法五:枚举遍历方法一
/*
Enumeration<Object> e1 = ha.elements();
while (e1.hasMoreElements())
{
System.out.println(e1.nextElement());
}
*/
// 方法六:枚举遍历方法二
/*
Enumeration<Object> e2 = ha.keys();
while (e2.hasMoreElements())
{
int key = (int) e2.nextElement();
System.out.println(key +"---"+ha.get(key));
}
*/
}
}
Hashmap和WeakHashMap也是一样的