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]
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]
System.out.println(c.remove(new Name1("1221", "11")));
System.out.println(c);
前一个输出false,后一个输出 [1221 11]
true
[]
结果输出的结果应该是:
true
[]可是我的上面输出的是
false
[1221 11]我用的是JDK1.6难道也JDK得版本 有关??