mark一下, 免的以后忘了
import java.util.*;
public class testCollection {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("Hello");
c.add(new Name("Aaron","qj"));
c.add(new Integer(100));
c.remove("Hello");
//c.remove(new Name("Aaron","qj"));
System.out.println(c.remove(new Name("Aaron", "qj")));
System.out.println(c.size());
System.out.println(c);
}
}
class Name{/*两个对象如果相等那么他们的Hashcode也应该相等*/
private String firstName,lastName;
public Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String toString(){
return firstName+" "+lastName;
}
public boolean equals(Object obj){
if(obj instanceof Name){
Name name = (Name)obj;
return (firstName.equals(name.firstName))&&(lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode(){
return lastName.hashCode();
}
}