import java.util.*;
public class BasicContainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Integer(100));
c.add(new Name("f1","11"));
c.remove("hello");
c.remove(new Integer(100));
c.remove(new Name("f1","11"));
System.out.println(c);
}
}
class Name {
private String firstName;
private String lastName;
Name(String firstName,String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String toString() {
return firstName + " " + lastName;
}
public boolean equals(Object obj) {
if (obj instanceof Name) { //如果obj是Name对象
Name name = (Name) obj; //强制转换成Name
return (firstName.equals(name.firstName))
&& (lastName.equals(name.lastName));
}
return super.equals(obj); //如果传进来的不是Name对象
}
public int hashCode() {
return firstName.hashCode();
}
}
尚学堂
最新推荐文章于 2019-05-24 22:06:16 发布