b******g 发帖数: 669 | 1 All set cares uniqueness and equals() determine whether two object are
identical.
但是我test 了下
import java.util.HashSet;
public class testT {
private String name;
public testT(String name){
this.name=name;
}
public boolean equals(Object o){
if(!(o instanceof testT))return false;
testT person=(testT)o;
return person.name.equals(this.name);
}
public static void main(String[] args){
HashSet hs=new HashSet();
testT p1= new testT("jing");
testT p2=new testT("jing");
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
System.out.println(p1.equals(p2));
for(Object obj:hs)
System.out.println(obj);
}
}
输出是:
2
true
testT@35960f05
testT@3487a5cc
为啥两个object 被认为是equals的,缺还能加在同一个hashset里? |
g*****g 发帖数: 34805 | 2 You need to override hashCode function as well.
【在 b******g 的大作中提到】 : All set cares uniqueness and equals() determine whether two object are : identical. : 但是我test 了下 : import java.util.HashSet; : public class testT { : private String name; : public testT(String name){ : this.name=name; : } : public boolean equals(Object o){
|
b******g 发帖数: 669 | 3 the statement is true:
the hash code method for a given class can be used to test for object
inequality,but not object equality, for that class.
hascode 和 inequality是什么关系?
【在 g*****g 的大作中提到】 : You need to override hashCode function as well.
|
g*****g 发帖数: 34805 | 4 A hashmap checks if an object exists by computing hashcode for the
object, inspect the bucket for given hashcode, and finally using
equals to determine equality.
【在 b******g 的大作中提到】 : the statement is true: : the hash code method for a given class can be used to test for object : inequality,but not object equality, for that class. : hascode 和 inequality是什么关系?
|
b******g 发帖数: 669 | 5 所以hashcode 不等,两个object 仍然可以equal。hascode 并不是equal的前提条件。
HashSet认为的uniqueness是hashcode相等同时equals() return ture.
我得再消化消化。谢谢。
【在 g*****g 的大作中提到】 : A hashmap checks if an object exists by computing hashcode for the : object, inspect the bucket for given hashcode, and finally using : equals to determine equality.
|
g*****g 发帖数: 34805 | 6 Well, you should always implement hashcode and equals at the same time.
That's a required practice, not doing that is calling trouble.
Two objects can have the same hashcode but not equal, not vice versa.
【在 b******g 的大作中提到】 : 所以hashcode 不等,两个object 仍然可以equal。hascode 并不是equal的前提条件。 : HashSet认为的uniqueness是hashcode相等同时equals() return ture. : 我得再消化消化。谢谢。
|
b******g 发帖数: 669 | 7 明白了.override 下 hashcode()就全明白了,多谢!
【在 g*****g 的大作中提到】 : You need to override hashCode function as well.
|
b******g 发帖数: 669 | 8 not vice versa??
two objects are equal but with different hashcode. is it ture?
【在 g*****g 的大作中提到】 : Well, you should always implement hashcode and equals at the same time. : That's a required practice, not doing that is calling trouble. : Two objects can have the same hashcode but not equal, not vice versa.
|
g*****g 发帖数: 34805 | 9 It should never happen, if it happens, it's a bug.
【在 b******g 的大作中提到】 : not vice versa?? : two objects are equal but with different hashcode. is it ture?
|
r***y 发帖数: 4379 | 10 跟 hashcode 没关系, 你的 equals() 方法实现错误
的 equals() 中 -- return person.name.equals(this.name);
比较的就是 string pool 中的静态string "jing"
你两次new testT 都指向同一个 string object
当然 equals() 是 true
另外, 即使你 new String("jing") 两次, 用 string 的equals 还会是 true
因为 String 的 equals 在比较它内涵的 char[]
are
【在 b******g 的大作中提到】 : All set cares uniqueness and equals() determine whether two object are : identical. : 但是我test 了下 : import java.util.HashSet; : public class testT { : private String name; : public testT(String name){ : this.name=name; : } : public boolean equals(Object o){
|
|
|
r*****l 发帖数: 2859 | 11 Goodbug is right. You always need to override equals() and hashCode()
together in such a way that:
1, If o1.equals(o2) returns true, o1.hashCode() MUST equal o2.hashCode().
2, if o1.hashCode() == o2.hashCode(), o1.equals(o2) can be true or false;
【在 g*****g 的大作中提到】 : It should never happen, if it happens, it's a bug.
|
r*****l 发帖数: 2859 | 12 The equals() can be written in this way as long as it has business sense,
and as long as hashCode() is written to conform this.
【在 r***y 的大作中提到】 : 跟 hashcode 没关系, 你的 equals() 方法实现错误 : 的 equals() 中 -- return person.name.equals(this.name); : 比较的就是 string pool 中的静态string "jing" : 你两次new testT 都指向同一个 string object : 当然 equals() 是 true : 另外, 即使你 new String("jing") 两次, 用 string 的equals 还会是 true : 因为 String 的 equals 在比较它内涵的 char[] : : are
|
z*******3 发帖数: 13709 | 13 你都重写equals了,而且直接调用这个方法
跟hashcode一点关系都没有了
只是理论上说这两个方法必需保持一致 |
z*******3 发帖数: 13709 | 14 突然想起来楼主在问什么了
楼主如果用这个方法做
String a = new String("1");
String b = "1";
HashSet set = new HashSet();
set.add(a);
set.add(b);
然后循环后的结果就是只有一个对象输出
所以hashcode其实就是给hashset做逻辑判断用的
所以只要是自定义的类,放到这个hashset等类里面去
就需要思考hashcode的问题
因为常用的几个类,String什么都重写过hashcode方法
保证同一内容输出的是同一个hashcode
而自定义的类的hashcode默认是每个对象都不一样
所以hibernate里面要重写hashcode |
z*******3 发帖数: 13709 | 15 其实说白了就是如何判断两个对象是一个东西
无论是hashcode还是equals
本质意义都是用来判断两个已经存在的对象是不是同一个实体
举个例子
一个学生只有一个学号
那么如果学号一致,那么肯定就是一个学生
这就对应数据库里面的主键
但是在多线程环境中,是很有可能产生这种重复的对象的
那么如何判断这两个是一个东西?
所以就需要你重写这两个方法
区别在于,hashcode是给collection用的
其它时候还是用equals判断
如果你不重写这两个方法
那么除非正好是同一个虚拟机上的同一个对象
也就是地址是一致的时候,才会被认为是同一个对象
其它时候,只要没有重写过这两个方法,就会被认为是不同对象
就好像一个学号有两个不同的人在用一样,这是不对的
尤其是对于自定义的对象来说
常用的String, Integer什么都重写过了 |