2016/1/22 3,将id为005的对象从集合中移除

 1 package shuzu;
 2 
 3 public class Emp {
 4 private String id;
 5 private String name;
 6 
 7 public Emp(String id, String name) {
 8     super();
 9     this.id = id;
10     this.name = name;
11 }
12 
13 public String getId() {
14     return id;
15 }
16 
17 public void setId(String id) {
18     this.id = id;
19 }
20 
21 public String getName() {
22     return name;
23 }
24 
25 public void setName(String name) {
26     this.name = name;
27 }
28 
29 
30 
31 }
 1 package shuzu;
 2 
 3 import java.util.*;
 4 
 5 public class TestMap {
 6 
 7     /*创建Map集合,创建Emp对象,并将创建的Emp对象添加到集合中
 8      * (Emp对象的id作为Map集合的键),并将id为005的对象从集合中移除*
 9      *
10      */
11     public static void main(String[] args) {
12         Emp id1=new Emp("001","狮子");
13         Emp id2=new Emp("002","老虎");
14         Emp id3=new Emp("003","斑马");
15         Emp id4=new Emp("004","羚羊");
16         Emp id5=new Emp("005","猎豹");
17         Emp id6=new Emp("006","大象");
18         
19         HashMap<String,String> Map=new HashMap<String,String>();
20         
21         Map.put(id1.getId(), id1.getName());
22         Map.put(id2.getId(), id2.getName());
23         Map.put(id3.getId(), id3.getName());
24         Map.put(id4.getId(), id4.getName());
25         Map.put(id5.getId(), id5.getName());
26         Map.put(id6.getId(), id6.getName());
27         
28         Set u=Map.keySet();
29         
30         System.out.println("Map集合中所有元素是: ");
31         Iterator t=u.iterator();
32        while(t.hasNext()){
33           String id =(String) t.next();
34           String name=Map.get(id);
35           System.out.println("第"+id+"号是"+name);
36        }
37        Map.remove("005");
38        System.out.println("去除005后Map集合中所有元素是: ");
39        Iterator ts=u.iterator();
40        while(ts.hasNext()){
41            String id =(String) ts.next();
42            String name=Map.get(id);
43            System.out.println("第"+id+"号是"+name);
44         }
45     }
46     }

posted on 2016-01-22 21:32  一棵树2016  阅读(851)  评论(0编辑  收藏  举报