package mypackage;
import java.util.* ;public class TestCollection2 {
public static void main(String[] args) {
Collection<Object> c1 = new HashSet<Object>() ;
//MyName mn = new MyName("lee","ch") ;
c1.add("hello") ;
c1.add("How") ;
c1.add(100) ;
c1.add(new Integer(200)) ;
c1.add(new MyName("lee","ch")) ;
boolean b1 = c1.remove("hello") ;
boolean b2 = c1.remove(new MyName("lee","ch")) ; //想要删除MyName("lee","ch")。
System.out.println(b1) ; //测试是否删除"hello",打印true,说明删除成功。
System.out.println(b2) ; //测试是否删除MyName("lee","ch"),打印false,说明删除未成功。急解决?????
//System.out.println(mn.equals(new MyName("lee","ch"))) ; //测试MyName类中的equals()方法是否重写成功。
System.out.println(c1) ; //打印c1集合里面的内容。
}}class MyName {
String firstName, lastName ;
public MyName(String firstName, String lastName) {
this.firstName = firstName ;
this.lastName = lastName ;
}
public String toString() { //重写toString()方法
return "lastName:"+lastName+" "+"firstName:"+firstName ;
}
public boolean equals(MyName name) { //重写equals(MyName name)方法,想要删除c1集合里面的MyName("lee","ch")
if(this.lastName.equals(name.lastName) &&(this.firstName.equals(name.firstName))) {
return true ;
}
else {
return false ;
}
}
public int hashCode() { //重写hashCode()方法
return firstName.hashCode() ;
}
}
import java.util.* ;public class TestCollection2 {
public static void main(String[] args) {
Collection<Object> c1 = new HashSet<Object>() ;
//MyName mn = new MyName("lee","ch") ;
c1.add("hello") ;
c1.add("How") ;
c1.add(100) ;
c1.add(new Integer(200)) ;
c1.add(new MyName("lee","ch")) ;
boolean b1 = c1.remove("hello") ;
boolean b2 = c1.remove(new MyName("lee","ch")) ; //想要删除MyName("lee","ch")。
System.out.println(b1) ; //测试是否删除"hello",打印true,说明删除成功。
System.out.println(b2) ; //测试是否删除MyName("lee","ch"),打印false,说明删除未成功。急解决?????
//System.out.println(mn.equals(new MyName("lee","ch"))) ; //测试MyName类中的equals()方法是否重写成功。
System.out.println(c1) ; //打印c1集合里面的内容。
}}class MyName {
String firstName, lastName ;
public MyName(String firstName, String lastName) {
this.firstName = firstName ;
this.lastName = lastName ;
}
public String toString() { //重写toString()方法
return "lastName:"+lastName+" "+"firstName:"+firstName ;
}
public boolean equals(MyName name) { //重写equals(MyName name)方法,想要删除c1集合里面的MyName("lee","ch")
if(this.lastName.equals(name.lastName) &&(this.firstName.equals(name.firstName))) {
return true ;
}
else {
return false ;
}
}
public int hashCode() { //重写hashCode()方法
return firstName.hashCode() ;
}
}
c1.add(name) ;boolean b2 = c1.remove(name)); //想要删除MyName("lee","ch")。这样可以
public boolean equals(Object obj) { // 重写equals(MyName
// name)方法,想要删除c1集合里面的MyName("lee","ch")
if(!(obj instanceof MyName)) {
return false;
}
MyName name = (MyName) obj;
if (this.lastName.equals(name.lastName)
&& (this.firstName.equals(name.firstName))) {
return true;
} else {
return false;
}
}加上@Override以后覆盖父类的方法。
if (obj instanceof MyName) {
// name)方法,想要删除c1集合里面的MyName("lee","ch")
MyName myName = (MyName) obj;
if (this.lastName.equals(myName.lastName)
&& (this.firstName.equals(myName.firstName))) {
return true;
} else {
return false;
}
}
return false; }
重写equals方法错误!
c1.add(name) ;boolean b2 = c1.remove(name));
1,重写equals方法。正解在上楼。
2,
MyName mMyname = new MyNmae("lee","ch");
c1.add(mMyname );
c1.remove(mMyname );