代码中的equals 直接true 为了让两个对象相等
还是remove不了
hashCode也凑合着写了一下 不知怎么写 可以注释掉 但是记得只有map接口才要重写hashCode方法现在要remove那个对象 请问有什么方法 谢谢
import java.util.*;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c=new HashSet();
c.add(new Peg1());
System.out.println(c);
System.out.println(c.remove(new Peg1()));
System.out.println(c);
/*Peg j=new Peg();
Peg k=new Peg();
System.out.println(j.equals(k));*/ ;
}
}
class Peg1{
public boolean equals(Object obj){
return true;
}
public int hashCode(){
return super.hashCode();
}
}
还是remove不了
hashCode也凑合着写了一下 不知怎么写 可以注释掉 但是记得只有map接口才要重写hashCode方法现在要remove那个对象 请问有什么方法 谢谢
import java.util.*;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c=new HashSet();
c.add(new Peg1());
System.out.println(c);
System.out.println(c.remove(new Peg1()));
System.out.println(c);
/*Peg j=new Peg();
Peg k=new Peg();
System.out.println(j.equals(k));*/ ;
}
}
class Peg1{
public boolean equals(Object obj){
return true;
}
public int hashCode(){
return super.hashCode();
}
}
import java.util.*;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c=new HashSet();
c.add(new Peg1());// a
System.out.println(c);
System.out.println(c.remove(new Peg1())); //首先你这里删除的对象和a处添加的不是同一个对象,他们引用的内存地址也不一样,
System.out.println(c); //这里输出的是a出添加的对象。
/*Peg j=new Peg();
Peg k=new Peg();
System.out.println(j.equals(k));*/ ;//因为你重写了Object的equals() 所以不管你传递什么参数,都是return true 恒成立。
}
}
class Peg1{
public boolean equals(Object obj){
return true;
}
public int hashCode(){
return super.hashCode(); //这里返回的是父类的内存地址。。
}
}
非正常办法
public int hashCode(){
return 1; //这样可以强制remove
} 还有一个问题 一般通过重写equals是可以remove成功的 不必重写hasshCode只有在map中 在重写equals时才有必要重写hashCode
so?
想要remove掉的话,就要活得set里面对象的引用
HashSet<String> set = new HashSet<String>();
String s1 = new String("aaa");
String s2 = s1;
set.add(s1);
System.out.println(set);
set.remove(s2);
System.out.println(set);
if(!"C".equals(ite.next())){
ite.remove();
}
}
所以这就是为什么你只让equals()返回true是不能remove掉的,同时这也是为什么java建议当两个对象相同时请确保他们的hashCode()也相同,建议对这两个方法的覆盖直接使用IDE生成的会比较好。
如果仅为测试,你可以将你的代码改一下
import java.util.*;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c=new HashSet();
c.add(new Peg1());
System.out.println(c);
System.out.println(c.remove(new Peg1()));
System.out.println(c);
/*Peg j=new Peg();
Peg k=new Peg();
System.out.println(j.equals(k));*/ ;
}
}
class Peg1{ //下面两个方法的覆盖,会导致所有Peg1对象都认为是同一个对象
public boolean equals(Object obj){
return true;
}
public int hashCode(){
return 1;
}
}
import java.util.HashSet;public class Test3
{
public static void main(String[] args)
{
Collection<Peg1> c = new HashSet<Peg1>(); Peg1 peg1 = new Peg1(); c.add(peg1);
System.out.println(c);
System.out.println(c.remove(peg1));//指向同一个对象就可以删除掉了,原例子new 一次都生成一次新对象,对象不一样
System.out.println(c);
}
}class Peg1
{
}
hascode 和 equals主要是为了添加时候区别是否同属性值的,相同的话拒绝添加.主要是区别方式不一样,hascode 要提供一个算法,这样可以直接比较算法值算法值相同的时候再调用equals.