Java 集合练习——3

创建Map集合,创建Emp对象,并将创建的Emp对象添加到集合中,并将id为005的对象从集合中移除

创建Emp类:

package jihe;

public class Emp {
	
	private String id;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	private String name;
	public Emp(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	

 创建Map类并测试:

package jihe;

import java.util.*;

public class lianxi1 {

	public static void main(String[] args) {
		Map<String,String> map=new HashMap<>();
		Emp emp=new Emp("001","汉堡");
		Emp emp1=new Emp("002","薯条");
		Emp emp2=new Emp("003","咖啡");
		Emp emp3=new Emp("004","牛奶");
		Emp emp4=new Emp("005","鸡翅");
		Emp emp5=new Emp("006","鸡腿");
		map.put(emp.getId(), emp.getName());
		map.put(emp1.getId(), emp1.getName());
		map.put(emp2.getId(), emp2.getName());
		map.put(emp3.getId(), emp3.getName());
		map.put(emp4.getId(), emp4.getName());
		map.put(emp5.getId(), emp5.getName());
		
        map.remove(emp4.getId());
		
		Set <String> k=map.keySet();
		Iterator<String> it=k.iterator();
		while(it.hasNext())
		{
			String str=it.next();
			System.out.println(str+"  "+map.get(str));
			
		}
		
	}

}

 测试结果:

posted @ 2016-05-30 19:28  jakermk  阅读(315)  评论(0编辑  收藏  举报