set接口

 1         Set s1=new HashSet();
 2         Set s2=new HashSet();
 3         s1.add("a");
 4         s1.add("b");
 5         s1.add("c");
 6         s2.add("d");
 7         s2.add("a");
 8         s2.add("b");
 9         Set sn=new HashSet(s1);
10         sn.retainAll(s2);
11         Set su=new HashSet(s1);
12         su.addAll(s2);
13         System.out.println(sn);
14         System.out.println(su);

 

 1 import java.util.*;
 2 
 3 class Name
 4 {
 5     private String firstName,lastName;
 6     
 7     public String getFirstName() {
 8         return firstName;
 9     }
10 
11     public void setFirstName(String firstName) {
12         this.firstName = firstName;
13     }
14 
15     public String getLastName() {
16         return lastName;
17     }
18 
19     public void setLastName(String lastName) {
20         this.lastName = lastName;
21     }
22     public Name(String firstName,String lastName)
23     {
24         this.firstName=firstName;
25         this.lastName=lastName;        
26     }
27     public boolean equals(Object obj)
28     {
29         if (obj instanceof Name)
30         {
31             Name name=(Name)obj;
32             return (firstName.equals(name.firstName)) && (lastName.equals(name.lastName));            
33         }
34         
35         return super.equals(obj);
36     }
37     public int hashCode()
38     {
39         return firstName.hashCode();
40     }
41     public String toString() {  return firstName + " " + lastName;  }    
42     
43 }
44 
45 
46 
47 public class testmy1 {
48     
49     public static void main(String[] args) 
50     {
51         Set s=new HashSet();
52         s.add("hello");
53         s.add("world");
54         s.add(new Name("f1","f2"));
55         s.add(new Integer(100));
56         s.add(new Name("f1","f2"));
57         s.add("hello");
58         System.out.println(s);
59     }
60 
61 }

posted @ 2013-04-06 11:14  glt66  阅读(332)  评论(0编辑  收藏  举报