Pg235课后习题

1.分别向Set集合以及List集合中添加“A”、“a”、“c”、“C”、“a”5个元素,观察重复值“a”能否在List集合以及Set集合中成功添加。

 1 package org.hanqi.array;
 2 
 3 import java.util.*;
 4 
 5 public class 实战1 {
 6 
 7     public static void main(String[] args) {
 8     
 9               List<Object> l=new ArrayList<Object>();
10         
11         l.add("A");
12         l.add("a");
13         l.add("c");
14         l.add("C");
15         l.add("a");
16         
17         if(l.size()==5)
18         {
19             System.out.println("List集合="+"重复值“a”在List集合中成功添加");
20         }
21         else
22         {
23             System.out.println("List集合="+"重复值“a”在List集合中未成功添加");
24         }
25         
26         
27         
28         HashSet<String> s=new HashSet<String>();
29         
30         s.add("A");
31         s.add("a");
32         s.add("c");
33         s.add("C");
34         s.add("a");
35     
36 
37         if(s.size()==5)
38         {
39             System.out.println("Set集合="+"重复值“a”在Set集合中成功添加");
40         }
41         else
42         {
43             System.out.println("Set集合="+"重复值“a”在Set集合中未成功添加");
44         }
45         
46     }
47 
48 }
判断

2.创建Map集合,创建Emp对象,并将创建的Emp对象添加到集合中(Emp对象的id作为Map集合的键),并将id为005的对象从集合中移除。

 1 package org.hanqi.array;
 2 
 3 import java.util.*;
 4 
 5 public class Emp {
 6     
 7     private String a_id;
 8     
 9     private String a_name;
10 
11     public String getA_id() {
12         return a_id;
13     }
14 
15 
16     public void setA_id(String a_id) {
17         this.a_id = a_id;
18     }
19 
20 
21     public String getA_name() {
22         return a_name;
23     }
24 
25 
26     public void setA_name(String a_name) {
27         this.a_name = a_name;
28     }
29 
30     public Emp(String a_id, String a_name) {
31         
32         this.a_id = a_id;
33         this.a_name = a_name;
34     }
35 
36 
37     public static void main(String[] args) {
38     
39            // Map map=new HashMap();
40             Map<String, String>map=new HashMap<String, String>();
41             
42             Emp emp=new Emp("024","科比");
43             Emp emp2=new Emp("023","詹姆斯");
44             Emp emp3=new Emp("008","林书豪");
45             Emp emp4=new Emp("005","姚明");
46                
47             map.put(emp.getA_id(), emp.getA_name());
48             map.put(emp2.getA_id(), emp2.getA_name());
49             map.put(emp3.getA_id(), emp3.getA_name());
50             map.put(emp4.getA_id(), emp4.getA_name());
51             
52             Set set=map.keySet();
53             
54             map.remove(emp4);
55             
56             for(String t: map.keySet())
57             {
58                 if(t=="005")
59                 {
60                     map.remove(emp4);
61                  }
62                 else
63                 {
64                      System.out.println(t);
65                 }
66             }
67     }
68 
69 }
Emp——Map

原文地址:https://www.cnblogs.com/arxk/p/5267386.html