import java.util.*;
public class TestBasic_collection{
public static void main(String[]args){
Collection c = new HashSet();
c.add("hello");
c.add(new Name1("1221", "11"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name1("1221", "11")));
System.out.println(c);
}
}class Name1{
 String firstName, lastName;
public Name1(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 firstName.hashCode();
}
}
输出结果应该是:false可怎么是:false [1221 11]

解决方案 »

  1.   

    看到没,你程序中有两个输出语句:
    System.out.println(c.remove(new Name1("1221", "11"))); 
    System.out.println(c); 
    前一个输出false,后一个输出 [1221 11]
      

  2.   

    可是  c.remove(new Name1("1221", "11")  是将它从容器中移出去就是说  现在容器中已经没有元素第二个输出语句中 应该什么也没输出 
      

  3.   

    用JDK 1.6.13,验证结果如下:
    true
    []
      

  4.   

    7楼的提醒我了  
    结果输出的结果应该是:
    true
    []可是我的上面输出的是
    false 
    [1221 11]我用的是JDK1.6难道也JDK得版本  有关??