码迷,mamicode.com
首页 > 其他好文 > 详细

14.7-3

时间:2015-10-16 23:20:27      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

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

14.7-3

标签:

原文地址:http://www.cnblogs.com/dirgo/p/4886610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!