import java.util.Collection;
import java.util.HashSet;public class TestContainer { public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Integer(100));
c.add(new Name("f1","l1"));

c.remove("hello");
c.remove(new Integer(100));

//Name n = new Name("f1","l1");
//c.remove(new Name("f1","l1"));


System.out.println(c.remove(new Name("f1","l1")));
System.out.println(c);


}}class Name  {
private String firstName,lastName;
public Name (String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getFristName() {return firstName;}
public String getLastName() {return lastName;}
public String toString(){
return firstName + " " +lastName;
}

public boolean equals(Object obj) {
if (obj instanceof Name){
Name n = (Name)obj;
return this.firstName.equals(n.firstName) && this.lastName.equals(n.lastName);
}
return super.equals(obj);
}


public int Hashcode (){
return firstName.hashCode();
}


}

解决方案 »

  1.   

    用Eclipse的自动生成equals和hashCode的功能。
    to know why: http://blog.csdn.net/michaellufhl/archive/2010/08/23/5833188.aspx
      

  2.   


    return this.firstName.equals(n.firstName) && this.lastName.equals(n.lastName);上面的代码访问类的私有属性了,所以出问题了,改成下面的就行了。return this.firstName.equals(n.getFristName()) && this.lastName.equals(n.getLastName());祝你好运!
      

  3.   

    3楼的还是不行啊···
    还是false 啊········麻烦大家了···再帮想想·
      

  4.   

    上面看错了,是你的hashCode的函数名写错了,看看下面的代码:import java.util.Collection;
    import java.util.HashSet;public class Test { public static void main(String[] args) {
    Collection c = new HashSet();
    c.add("hello");
    c.add(new Integer(100));
    c.add(new Name("f1", "l1")); c.remove("hello");
    c.remove(new Integer(100)); // Name n = new Name("f1","l1");
    // c.remove(new Name("f1","l1")); System.out.println(c.remove(new Name("f1", "l1")));
    System.out.println(c); }}class Name {
    private String firstName, lastName; public Name(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    } public String getFristName() {
    return firstName;
    } public String getLastName() {
    return lastName;
    } public String toString() {
    return firstName + " " + lastName;
    } @Override
    public boolean equals(Object obj) {
    if (obj instanceof Name) {
    Name n = (Name) obj;
    return this.firstName.equals(n.getFristName())
    && this.lastName.equals(n.getLastName());
    }
    return super.equals(obj);
    } //函数名写错了,你的是Hashcode,父类中的是hashCode
    @Override
    public int hashCode() {
    return firstName.hashCode();
    }
    }
      

  5.   

    你先new2个Name对象,然后输出一下equals比较的结果吧。return super.equals(obj);
    这个没必要吧。 if不满足直接return false;
      

  6.   

    Joshua Bloch :如何写一个优质的hashCode()函数
    1、给int变量result赋予某个非0常量的,例如17(质数17很神奇)2、为你的对象内每个有意义的域f(每个可以做equals()操作的域)计算一个int散列码 c :域类型:boolean--> c = (f ? 0:1)
    域类型:byte\char\short\int--> c = (int) f
    域类型:long--> c= (int)(f ^ ( f >>>32 ))
    域类型:float--> c =  Float.floatToIntBits(f)
    域类型:double --> long l = Double.doubleToLongBits(f);
                                     c=(int)(l ^ (l >>>32 ) ) 
    域类型:Object,其equals () 调用这个域的 equals()--> c= f.hashCode()
    3、合并计算得到的散列码:resule  =37*result + c ;4、返回result。例子:class Name {
      private String firstName,lastName;  public Name (String firstName,String lastName){
           this.firstName = firstName;
           this.lastName = lastName;
      }
    ....  @Override
       public int Hashcode (){
           int result = 17;
           result = 37 * result + firstName.hashCode();
           result = 37 * result + lastName.hashCode();
           return result;
      }
    }
      

  7.   

    hashCode函数名写错了,是hashCode 不是HashCode更改时候好用了.