问题

 1 package Demo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.HashMap;
 6 import java.util.HashSet;
 7 import java.util.LinkedList;
 8 import java.util.Map;
 9 
10 import javax.sound.midi.Sequence;
11 
12 public class Set {
13 
14     public static void main(String[] args) {
15         // TODO Auto-generated method stub
16         Collection c = new ArrayList();
17          c.add("hello");
18         // c.add(new Integer("100"));
19         c.add(new Name("a", "b"));
20         System.out.println(c.hashCode());
21         c.add(new Name("a", "b"));
22         
23         // c.remove("hello");
24         // c.remove(new Integer("100"));
25         System.out.println(c.remove(new Name("a", "b")));
26         System.out.println(c);    
27         
28     }
29 }
30 
31 class Name extends Object {
32     private String firstname;
33     private String lastname;
34 
35     public Name(String firstname, String lastname) {
36         this.firstname = firstname;
37         this.lastname = lastname;
38     }
39 
40     public String getFirstname() {
41         return firstname;
42     }
43 
44     public String getLastname() {
45         return lastname;
46     }
47 
48     public String toString() {
49         return "the firstname is " + firstname + " the last name is " + lastname;
50     }
51 
52     public boolean equals(Name a) {
53         return ((this.firstname.equals(a.firstname)) && (this.lastname.equals(a.lastname)));
54 
55     }
56 //     public boolean equals(Object obj) {
57 //     if (obj instanceof Name) {
58 //     Name d = (Name) obj;
59 //     return (this.firstname.equals(d.firstname)) &&
60 //     (this.lastname.equals(d.lastname));
61 //     } else
62 //     return super.equals(obj);
63 //     }
64 
65     public int hashCode() {
66         return firstname.hashCode();
67     }
68 }
原文地址:https://www.cnblogs.com/laigaoxiaode/p/5586137.html