MangeEmpHashMap
package com.emp;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.plaf.basic.BasicScrollPaneUI.HSBChangeListener;
public class MangeEmpHashMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap hm=new HashMap();
Emp em1=new Emp("s001", "rong",3.5f);
Emp em2=new Emp("s002", "pei", 4.0f);
//把对象和编号放入hm
hm.put("s001", em1);
hm.put("s002", em2);
if(hm.containsKey("s002"))
{
System.out.println("有该员工");
Emp emp=(Emp)hm.get("s002");
System.out.println("name:"+emp.getName());
System.out.println("sal:"+emp.getSal());
}else {
System.out.println("没有该员工");
}
System.out.println("@@@@@@@");
//迭代
Iterator it=hm.keySet().iterator();
while (it.hasNext())
{
String key=it.next().toString();
Emp em=(Emp)hm.get(key);
System.out.println("name:"+em.getName());
}
}
}
Emp类在在上一篇,Arraylist已经定义,同一个包的类没有特别说明的话可以共用!