import java.util.*;public class BasicContainer{
public static void main(String[] args){
Collection c=new HashSet();
c.add("hello");
c.add(new Name("l1","f1"));
c.add(new Integer(100));
System.out.println(c);
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("l1","f1")));
System.out.println(c);
String s1=new String("A");
String s2=new String("A");
System.out.println(s1.equals(s2));
}
}class Name{
private String lastName,firstName;
Name(String ln,String fn){
lastName=ln;
firstName=fn;
} public String getlastName(){
return lastName;
} public String getfirstName(){
return firstName;
} public String toString(){
return lastName+" "+firstName;
} public boolean equals(Object obj){
if(obj instanceof Name){
Name name=(Name)obj;
return (lastName==name.lastName) && (firstName==name.firstName);
}//为什么这里写==和使用equals()是等价的
return super.equals(obj);
} public int hashCode(){
return firstName.hashCode();
}  

解决方案 »

  1.   

    HashSet里调用remove方法是去调用传进去参数的equals方法,总之有一点:如果你重写equals方法就是为了实现比较内容,不重写的话就是调用Object类里面的equals方法(还是比较地址),String类已经重写了equals方法
      

  2.   


    import java.util.*;public class BasicContainer{
        public static void main(String[] args){
            Collection c=new HashSet();
            c.add("hello");
            c.add(new Name("l1","f1"));
            c.add(new Integer(100));
            System.out.println(c);
            c.remove("hello");
            c.remove(new Integer(100));
            System.out.println(c.remove(new Name(new String("l1"),new String("f1"))));        System.out.println(c);
            String s1=new String("A");
            String s2=new String("A");
            System.out.println(s1.equals(s2));
        }
    }class Name{
        private String lastName,firstName;
        Name(String ln,String fn){
            lastName=ln;
            firstName=fn;
        }    public String getlastName(){
            return lastName;
        }    public String getfirstName(){
            return firstName;
        }    public String toString(){
            return lastName+" "+firstName;
        }    public boolean equals(Object obj){
            if(obj instanceof Name){
                Name name=(Name)obj;
            return (lastName==name.lastName) && (firstName==name.firstName);
            }
            return super.equals(obj);
        }    public int hashCode(){
            return firstName.hashCode();
        }     
    }
    结果
    [100, hello, l1 f1]
    false
    [l1 f1]
    true
    改了System.out.println(c.remove(new Name(new String("l1"),new String("f1")))
    那个地方必须使用equal,你用==对了是因为比较的两个字符串是在String池中取的同一个字符串,所以地址一样
      

  3.   

    在集合类中的 equals()方法是比较两个对象的值是否相等 (内容)
    == 是指的是比较的对象的地址是不相等
    你自己在参考哈
      

  4.   

    ==比较地址,有时相等是因为地址相同。比如说Integer类型的-127-128之类的。
      

  5.   

         equals 方法是 Object 类提供的一个实例方法 ,所有的引用变量都可以调用该方法来判断是否与其他引用变量相等,但这个方法判断两个对象相等的标准与==符号没有区别,同样要求两个引用变量指向同一个对象才会返回true。 Object 提供的equals()方法只是比较对象的地址,所以一样!要是想要==和equals()不同的话,那就要重写equals()方法了,这样就可以到达两者不同的目的了!