package com.hanqi.jihe; import java.util.*; public class emplei { //先创建emplei类 //定义类型 private String eid; private String ename; public emplei(String eid,String ename) { this.eid=eid; this.ename=ename; } public String getEid() { return eid; } public void setEid(String eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } }
package com.hanqi.jihe; import java.util.*; public class Mapceshi { public static void main(String[] args) { //创建一个map集合 Map<String, String> mp = new HashMap<>(); //创建emplei对象, emplei em1 = new emplei("001","1"); emplei em2 = new emplei("002","2"); emplei em3 = new emplei("003","3"); emplei em4 = new emplei("004","4"); emplei em5 = new emplei("005","5"); //将对象添加到mp集合中 mp.put(em1.getEid(), em1.getEname()); mp.put(em2.getEid(), em2.getEname()); mp.put(em3.getEid(), em3.getEname()); mp.put(em4.getEid(), em4.getEname()); mp.put(em5.getEid(), em5.getEname()); //遍历mp集合 Set <String> k =mp.keySet(); for(String i:k) { System.out.println(i+" "+mp.get(i)); } //移除id为005的对象 mp.remove("005"); System.out.println(); //迭代器遍历 Iterator<String> it = k.iterator(); while(it.hasNext()) { String str = it.next(); System.out.println(str+" "+mp.get(str)); } } }