标签:
Emp类
1 package jihe; 2 3 public class Emp { 4 5 public String id; 6 public String name; 7 8 public Emp(String id, String name) 9 { 10 this.id = id; 11 this.name =name; 12 } 13 14 }
Test类
1 package jihe; 2 3 import java.util.*; 4 5 public class Test { 6 7 public static void main(String[] args) 8 { 9 10 //创建Map集合 11 Map<String,String> map = new HashMap<>(); 12 13 //创建Emp对象 14 Emp emp = new Emp("011","aaa"); 15 Emp emp1 = new Emp("012","bbb"); 16 Emp emp2 = new Emp("013","ccc"); 17 Emp emp3 = new Emp("014","ddd"); 18 Emp emp4 = new Emp("015","eee"); 19 Emp emp5 = new Emp("016","fff"); 20 21 //将Emp对象添加到Map集合中 22 map.put(emp.id, emp.name); 23 map.put(emp1.id, emp1.name); 24 map.put(emp2.id, emp2.name); 25 map.put(emp3.id, emp3.name); 26 map.put(emp4.id, emp4.name); 27 map.put(emp5.id, emp5.name); 28 29 //移除id为015的对象 30 map.remove("015"); 31 32 //遍历集合,打印 33 Set<String> set = map.keySet(); 34 for(String id : set) 35 { 36 System.out.println("id = " + id + " name = " + map.get(id)); 37 } 38 39 } 40 41 }
运行结果
id = 011 name = aaa
id = 012 name = bbb
id = 013 name = ccc
id = 014 name = ddd
id = 016 name = fff
标签:
原文地址:http://www.cnblogs.com/dirgo/p/4886610.html