创建Map集合,创建Emp对象,并将创建的Emp对象田间道集合中(Emp对象中的id作为Map集合的键),并将id为005的对象从集合中移除。
package com.hanqi.jihe; public class Emp { private String e_id; private String e_name; public Emp(String e_id,String e_name) { this.e_id=e_id; this.e_name=e_name; } public String getE_id() { return e_id; } public void setE_id(String e_id) { this.e_id = e_id; } public String getE_name() { return e_name; } public void setE_name(String e_name) { this.e_name = e_name; } }
package com.hanqi.jihe; import java.util.*; public class TestMap2 { public static void main(String[] args) { Map<String,String> map=new HashMap<>(); Emp e=new Emp("001","一"); Emp e2=new Emp("003","三"); Emp e3=new Emp("005","五"); Emp e4=new Emp("007","七"); map.put(e.getE_id(), e.getE_name()); map.put(e2.getE_id(), e2.getE_name()); map.put(e3.getE_id(), e3.getE_name()); map.put(e4.getE_id(), e4.getE_name()); map.remove("005"); Set set=map.keySet(); Iterator<String>it=set.iterator(); while(it.hasNext()) { String s=it.next(); String n=map.get(s); System.out.println(s+" "+n); } } }