由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问个Object.hashCode()的问题
相关主题
Object比较问个set和literal String的问题
treemap和hashma p的问题Re: 谁有Java或Oracle的毒招 ?
也问个 HashMap问题Do I need to implement equals and hashCode in domain object
问个hashtable实现问题问HashSet的问题?
请问一个serialize class object下载运行的问题how to copy an Object?
answer Re: how HashMap/Hashtable compare key?java gc之前有没有办法得到notification?
Java的工作面试主要考什么?主要是算法吗?[转载] How do I sort a map in Java?
请教一段代码,关于hashCode()Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了
相关话题的讨论汇总
话题: hashcode话题: override话题: hashmap话题: equals话题: object
进入Java版参与讨论
1 (共1页)
S**********C
发帖数: 161
1
书上说,一般Override equals()的时候要同时Override hashCode()
那么,如果我
1)不Override hashCode()
2)Override hashCode(),但返回一个常数,比如10
3)Override hashCode(),但随机产生一个数,而不是根据
一般的根据Object里面的Fields(e.g Employee(employeeId,name)来定
以上三种情况,后果分别是什么?Java是如何判断两个
Object是否"相等",只要equals() return true,还是同时要
hashCode()返回相同的值?
F****n
发帖数: 3271
2
hashCode() has nothing to do with equals() technically
but conventionally if equals() return true then the other object must return
the same hashCode(). Otherwise a lot of Java programs based on this
convention (like HashMap) will mess up.

HashMap will mess up.
HashMap will not mess up but performance is done.
HashMap will mess up.
That's the convention

【在 S**********C 的大作中提到】
: 书上说,一般Override equals()的时候要同时Override hashCode()
: 那么,如果我
: 1)不Override hashCode()
: 2)Override hashCode(),但返回一个常数,比如10
: 3)Override hashCode(),但随机产生一个数,而不是根据
: 一般的根据Object里面的Fields(e.g Employee(employeeId,name)来定
: 以上三种情况,后果分别是什么?Java是如何判断两个
: Object是否"相等",只要equals() return true,还是同时要
: hashCode()返回相同的值?

S**********C
发帖数: 161
3
I understand that, the question is what happened if it doesn't return the
same hashCode as in my post, I know that HashMap/HashSet use the hasCode to
locate the bucket, the real question becomes, what do you mean by "mess up",
it cannot locate the bucket, or it may add duplicate records in different
bucket? but again, if equals is the same, how can duplicate records be added?
Also, how about the situation when this object is not inserted into
any collection, do any of the answers changed?

return

【在 F****n 的大作中提到】
: hashCode() has nothing to do with equals() technically
: but conventionally if equals() return true then the other object must return
: the same hashCode(). Otherwise a lot of Java programs based on this
: convention (like HashMap) will mess up.
:
: HashMap will mess up.
: HashMap will not mess up but performance is done.
: HashMap will mess up.
: That's the convention

g*****g
发帖数: 34805
4
相等是查equals,不要求hashCode返回同样的值。
但是hashCode如果不返回同样的值,在HashMap一类的数据结构里
会出问题。会出现
a.equals(b) == true
hashMap.put(a, blah);
hashMap.get(b) == null;
所以你hashCode返回一个常数都没事,只不过哈希效率极低而已,
但返回不同的值就会出问题。

【在 S**********C 的大作中提到】
: 书上说,一般Override equals()的时候要同时Override hashCode()
: 那么,如果我
: 1)不Override hashCode()
: 2)Override hashCode(),但返回一个常数,比如10
: 3)Override hashCode(),但随机产生一个数,而不是根据
: 一般的根据Object里面的Fields(e.g Employee(employeeId,name)来定
: 以上三种情况,后果分别是什么?Java是如何判断两个
: Object是否"相等",只要equals() return true,还是同时要
: hashCode()返回相同的值?

S**********C
发帖数: 161
5
Thanks, beside HashMap/Collection, is there any other circumstances that
we need to override equals() and hashCode()?
For example, any objectsng that need to searilze?
like web service or hibernate mapping class? If yes,what will happen
if the hashCode doesn't return the consistent value?

【在 g*****g 的大作中提到】
: 相等是查equals,不要求hashCode返回同样的值。
: 但是hashCode如果不返回同样的值,在HashMap一类的数据结构里
: 会出问题。会出现
: a.equals(b) == true
: hashMap.put(a, blah);
: hashMap.get(b) == null;
: 所以你hashCode返回一个常数都没事,只不过哈希效率极低而已,
: 但返回不同的值就会出问题。

F****n
发帖数: 3271
6
That's a convention in Java and should be regarded as part of the
specification. You need to respect it no matter what because there is always
a chance other programs will assume it.

【在 S**********C 的大作中提到】
: Thanks, beside HashMap/Collection, is there any other circumstances that
: we need to override equals() and hashCode()?
: For example, any objectsng that need to searilze?
: like web service or hibernate mapping class? If yes,what will happen
: if the hashCode doesn't return the consistent value?

b******y
发帖数: 1684
7
这么说把,不遵守这个convention的object是次品

always

【在 F****n 的大作中提到】
: That's a convention in Java and should be regarded as part of the
: specification. You need to respect it no matter what because there is always
: a chance other programs will assume it.

f****n
发帖数: 208
8
次品 is even worse than 废品,because you never know when it will have
problem, making it a nightmare to debug.
Following the convention is very important in enterprise programming, a lot
of times a smart idea is a bad solution.
S**********C
发帖数: 161
9
我知道这个是个Convention,最好要Follow,这么说吧,这
是一个面试的时候别人问到的题目,我答得跟你们说的都差不多,
但好像面试的人仍然要追问,如果不这么做,会发生什么,
我回头看了你回答的第1)个,如果不Override,不应该就用
Object defatult的hashCode() 吗?
我查了一下源代码,equals() default implementation是 return this == obj,
也就是比较两个obj在内存里面是否指向同一个reference,但
hashCode() default implementation是一个native function,不知道
里面是怎么实现的,default会返回什么数值,是否也是
一个常数,那就变成问题2)? 还有对于会造成mess up,面试
的会追问怎么个mess up法,是容许duplicate了呢,还是put了
之后想get的时候返回不同值?那如果不放在HashMap里呢,比如
一个Seariable的Object,是否有同样的问题?
我觉得搞清楚这点还是有点意义的,决不是"Just a convention,
follow it"这么简单。也不完全是回字的4种写法这么无聊。

always

【在 F****n 的大作中提到】
: That's a convention in Java and should be regarded as part of the
: specification. You need to respect it no matter what because there is always
: a chance other programs will assume it.

g*****g
发帖数: 34805
10
It will mess up hashmap, that's what the interviewer wanted to hear.
The piece of code I wrote in the earlier post demos that.
Serialization/deserialization has more to do with serialVersionUID.
and Serializable interface if it's implemented
I don't think hashCode is related.

【在 S**********C 的大作中提到】
: 我知道这个是个Convention,最好要Follow,这么说吧,这
: 是一个面试的时候别人问到的题目,我答得跟你们说的都差不多,
: 但好像面试的人仍然要追问,如果不这么做,会发生什么,
: 我回头看了你回答的第1)个,如果不Override,不应该就用
: Object defatult的hashCode() 吗?
: 我查了一下源代码,equals() default implementation是 return this == obj,
: 也就是比较两个obj在内存里面是否指向同一个reference,但
: hashCode() default implementation是一个native function,不知道
: 里面是怎么实现的,default会返回什么数值,是否也是
: 一个常数,那就变成问题2)? 还有对于会造成mess up,面试

v****s
发帖数: 1112
11
i screwed up this interview question...
the guy asked me, what kind of harm will u get if override a hashcode()
function,
i though it was related to gc.... he mumbled a bit%&*&(*&(*# and we moved to
next question....

【在 g*****g 的大作中提到】
: It will mess up hashmap, that's what the interviewer wanted to hear.
: The piece of code I wrote in the earlier post demos that.
: Serialization/deserialization has more to do with serialVersionUID.
: and Serializable interface if it's implemented
: I don't think hashCode is related.

F****n
发帖数: 3271
12
You cannot enumerate all cases and should not try to do so, because how can
you be sure about other programs' implementation? What you do is to
understand the mechanism and use your brain to think about each case.
And I think I told you as long as equals() return this == obj, you can
return any constants as hashCode(). It will only drop the performance.

【在 S**********C 的大作中提到】
: 我知道这个是个Convention,最好要Follow,这么说吧,这
: 是一个面试的时候别人问到的题目,我答得跟你们说的都差不多,
: 但好像面试的人仍然要追问,如果不这么做,会发生什么,
: 我回头看了你回答的第1)个,如果不Override,不应该就用
: Object defatult的hashCode() 吗?
: 我查了一下源代码,equals() default implementation是 return this == obj,
: 也就是比较两个obj在内存里面是否指向同一个reference,但
: hashCode() default implementation是一个native function,不知道
: 里面是怎么实现的,default会返回什么数值,是否也是
: 一个常数,那就变成问题2)? 还有对于会造成mess up,面试

f****n
发帖数: 208
13
Let me guess what the interviewer want to know one by one:
1) It's java convention that if you override equals method, you should also
override hashcode method. In real situation, it is always true. Otherwise
you don't override either.
2) I think the default hash function should give memory address instead of
constant, because it is much more efficient. So default equals and hashcode
compare the same thing.
3) Constant returned hashcode method is OK (compliable), but actually not a good choice. You are not using hashing function any more.
4) You mess up big (can lose your job as a java developer) if you have a
hashcode method returns different values on objects that equals. As showed
by goodbug's example.
5) Duplicate is fine for hash function, as long as not too much duplication
like constants.
6)You can read String class source code to see how its hashcode method is implemented as an
example. A hashcode method should only use the object fields that are used by its
equals method.
7)What will happen if hashmap object is serialized? Hashcode method can
return different values before and after serialization. However the whole
hash table is reconstructed after serialization, so serialization won't mess
up a hashmap object.

【在 S**********C 的大作中提到】
: 我知道这个是个Convention,最好要Follow,这么说吧,这
: 是一个面试的时候别人问到的题目,我答得跟你们说的都差不多,
: 但好像面试的人仍然要追问,如果不这么做,会发生什么,
: 我回头看了你回答的第1)个,如果不Override,不应该就用
: Object defatult的hashCode() 吗?
: 我查了一下源代码,equals() default implementation是 return this == obj,
: 也就是比较两个obj在内存里面是否指向同一个reference,但
: hashCode() default implementation是一个native function,不知道
: 里面是怎么实现的,default会返回什么数值,是否也是
: 一个常数,那就变成问题2)? 还有对于会造成mess up,面试

x*****p
发帖数: 1707
14
Suppose a and b are two objects, Java uses the following condition to
check whether a and b are duplicated objects.
a.hashCode()==b.hashCode() && a==b || a.equals(b)

【在 S**********C 的大作中提到】
: 书上说,一般Override equals()的时候要同时Override hashCode()
: 那么,如果我
: 1)不Override hashCode()
: 2)Override hashCode(),但返回一个常数,比如10
: 3)Override hashCode(),但随机产生一个数,而不是根据
: 一般的根据Object里面的Fields(e.g Employee(employeeId,name)来定
: 以上三种情况,后果分别是什么?Java是如何判断两个
: Object是否"相等",只要equals() return true,还是同时要
: hashCode()返回相同的值?

1 (共1页)
进入Java版参与讨论
相关主题
Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了请问一个serialize class object下载运行的问题
请问这个面试题,关于synchronize hashmapanswer Re: how HashMap/Hashtable compare key?
How to know the size of a java object ?Java的工作面试主要考什么?主要是算法吗?
请教一个语法和递归的问题请教一段代码,关于hashCode()
Object比较问个set和literal String的问题
treemap和hashma p的问题Re: 谁有Java或Oracle的毒招 ?
也问个 HashMap问题Do I need to implement equals and hashCode in domain object
问个hashtable实现问题问HashSet的问题?
相关话题的讨论汇总
话题: hashcode话题: override话题: hashmap话题: equals话题: object