Redian新闻
>
AMR 开始俯冲了
avatar
AMR 开始俯冲了# Stock
s*e
1
今天面试,被问到HashCode 和equals方法。
我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
处理?
这个两个对象还算是相同么?
avatar
k*r
2
还好昨天割了,大家看啥价位接呢?
avatar
s*j
3
That means equals() is problematic, it will cause unexpected result
especially in collection operation.
avatar
s*s
4
不割也不AD的飘过
avatar
h*e
5
说明 发生了hash冲突 collison就好比 桶形hash 不同元素经过hash 都打在了key
为代表的一个桶里,但是可能是桶上链表上的不同value.
avatar
k*n
6
sui bian cao,,,,no reaction!
avatar
s*e
7
那么请问对于这种情况,是属于hashcode方法设计的不对呢,还是equals方法设计不对?
如果equals方法是 false, 但是hashcode方法返回的是相等。那么应该修改哪一个方法
呢?
该如何解决这个问题呢?
请详细说说,谢谢!

【在 s*****j 的大作中提到】
: That means equals() is problematic, it will cause unexpected result
: especially in collection operation.

avatar
k*n
8
I buy some for daytrade @ 6.69 now
avatar
w*r
9
没有问题啊,hashcode可以一样,而equals不一样,概率很低,但是还是有可能有
但是equals一样,那么hashcode就肯定一样了
要不然就是写方法的那个家伙有问题
avatar
s*s
10
楼上的在闭着眼睛默默的享受?
avatar
h*e
11
“但是equals一样,那么hashcode就肯定一样了“ 这是不见得的,应该看具体怎
么定义相等。有可能equals 定义比hash算法严 equals是 hash 的子集,   也有
可能 equals和 hash 算法定义的有交集还有差集。

【在 w****r 的大作中提到】
: 没有问题啊,hashcode可以一样,而equals不一样,概率很低,但是还是有可能有
: 但是equals一样,那么hashcode就肯定一样了
: 要不然就是写方法的那个家伙有问题

avatar
k*r
12
昨天割肉割了账户的10%,那叫一个疼啊...

【在 k********n 的大作中提到】
: sui bian cao,,,,no reaction!
avatar
s*r
13
这个两个对象不相同 if equals returns false
如果两个对象 hashcode值一样,equals方法返回false,那么该如果处理?
需要改进你的hashCode() programming, 尽管equals方法返回false, 两个对象
hashcode值也可以一样, 但不是好的hashCode() implementation,而且会影响的
hashtable的performance,需要改进,

【在 s********e 的大作中提到】
: 今天面试,被问到HashCode 和equals方法。
: 我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
: 但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
: 处理?
: 这个两个对象还算是相同么?

avatar
P*B
14
have a stop?

【在 k********n 的大作中提到】
: I buy some for daytrade @ 6.69 now
avatar
m*k
15
this is expected, this is why you need define your own equals() if you wanna
use the obj as key in HashMap,
after the same hashcode leads you to the same bucket, you need equals() to
find the obj that matches your input so to retrieve the right value.
放狗, 你一下就明白了。
比如
http://javarevisited.blogspot.com/2013/08/10-equals-and-hashcod

对?

【在 s********e 的大作中提到】
: 那么请问对于这种情况,是属于hashcode方法设计的不对呢,还是equals方法设计不对?
: 如果equals方法是 false, 但是hashcode方法返回的是相等。那么应该修改哪一个方法
: 呢?
: 该如何解决这个问题呢?
: 请详细说说,谢谢!

avatar
c*m
16
re, 到5.* 我會AD

【在 s***s 的大作中提到】
: 不割也不AD的飘过
avatar
j*3
17
我也被问到过!!我也不会!
avatar
p*p
18
大盘这样还敢daytrade?而且还trade一个半死不活的东西

【在 k********n 的大作中提到】
: I buy some for daytrade @ 6.69 now
avatar
g*g
19
equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
例子来说hash function实现有问题。

【在 h*******e 的大作中提到】
: “但是equals一样,那么hashcode就肯定一样了“ 这是不见得的,应该看具体怎
: 么定义相等。有可能equals 定义比hash算法严 equals是 hash 的子集,   也有
: 可能 equals和 hash 算法定义的有交集还有差集。

avatar
h*e
20
恩又想了一下这个应该是对的,“equals为 true, hashcode必须相等,否则是bug. 反
之不必须"
后面那半句 hash function 有问题我没说过的,是别的版友说的, 我的equals 的
概念之前不太清。

【在 g*****g 的大作中提到】
: equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
: 例子来说hash function实现有问题。

avatar
y*a
21
没有问题,以下是 hashcode 的 contract:
Whenever it is invoked on the same object more than once during
an execution of a Java application, the {@code hashCode} method
must consistently return the same integer, provided no information
used in {@code equals} comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
If two objects are equal according to the {@code equals(Object)}
method, then calling the {@code hashCode} method on each of
the two objects must produce the same integer result.
It is not required that if two objects are unequal
according to the {@link java.lang.Object#equals(java.lang.Object)}
method, then calling the {@code hashCode} method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.

【在 s********e 的大作中提到】
: 今天面试,被问到HashCode 和equals方法。
: 我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
: 但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
: 处理?
: 这个两个对象还算是相同么?

avatar
s*t
22

同意这个,equals等了,hashcode怎么可能不等啊!! hash同一个key,value能不一样
???这还叫神马hash function啊

【在 g*****g 的大作中提到】
: equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
: 例子来说hash function实现有问题。

avatar
s*e
23
xie xie. Share with others.
http://javarevisited.blogspot.co.uk/2011/02/how-hashmap-works-i
http://javarevisited.blogspot.co.uk/2011/07/java-multi-threadin
http://javarevisited.blogspot.sg/2011/04/top-20-core-java-inter
http://javarevisited.blogspot.sg/2012/02/what-is-race-condition
http://javarevisited.blogspot.co.uk/2011/04/synchronization-in-
http://javarevisited.blogspot.co.uk/2010/10/why-string-is-immut

wanna

【在 m*****k 的大作中提到】
: this is expected, this is why you need define your own equals() if you wanna
: use the obj as key in HashMap,
: after the same hashcode leads you to the same bucket, you need equals() to
: find the obj that matches your input so to retrieve the right value.
: 放狗, 你一下就明白了。
: 比如
: http://javarevisited.blogspot.com/2013/08/10-equals-and-hashcod
:
: 对?

avatar
s*e
24
今天面试,被问到HashCode 和equals方法。
我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
处理?
这个两个对象还算是相同么?
avatar
s*j
25
That means equals() is problematic, it will cause unexpected result
especially in collection operation.
avatar
h*e
26
说明 发生了hash冲突 collison就好比 桶形hash 不同元素经过hash 都打在了key
为代表的一个桶里,但是可能是桶上链表上的不同value.
avatar
s*e
27
那么请问对于这种情况,是属于hashcode方法设计的不对呢,还是equals方法设计不对?
如果equals方法是 false, 但是hashcode方法返回的是相等。那么应该修改哪一个方法
呢?
该如何解决这个问题呢?
请详细说说,谢谢!

【在 s*****j 的大作中提到】
: That means equals() is problematic, it will cause unexpected result
: especially in collection operation.

avatar
w*r
28
没有问题啊,hashcode可以一样,而equals不一样,概率很低,但是还是有可能有
但是equals一样,那么hashcode就肯定一样了
要不然就是写方法的那个家伙有问题
avatar
h*e
29
“但是equals一样,那么hashcode就肯定一样了“ 这是不见得的,应该看具体怎
么定义相等。有可能equals 定义比hash算法严 equals是 hash 的子集,   也有
可能 equals和 hash 算法定义的有交集还有差集。

【在 w****r 的大作中提到】
: 没有问题啊,hashcode可以一样,而equals不一样,概率很低,但是还是有可能有
: 但是equals一样,那么hashcode就肯定一样了
: 要不然就是写方法的那个家伙有问题

avatar
s*r
30
这个两个对象不相同 if equals returns false
如果两个对象 hashcode值一样,equals方法返回false,那么该如果处理?
需要改进你的hashCode() programming, 尽管equals方法返回false, 两个对象
hashcode值也可以一样, 但不是好的hashCode() implementation,而且会影响的
hashtable的performance,需要改进,

【在 s********e 的大作中提到】
: 今天面试,被问到HashCode 和equals方法。
: 我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
: 但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
: 处理?
: 这个两个对象还算是相同么?

avatar
m*k
31
this is expected, this is why you need define your own equals() if you wanna
use the obj as key in HashMap,
after the same hashcode leads you to the same bucket, you need equals() to
find the obj that matches your input so to retrieve the right value.
放狗, 你一下就明白了。
比如
http://javarevisited.blogspot.com/2013/08/10-equals-and-hashcod

对?

【在 s********e 的大作中提到】
: 那么请问对于这种情况,是属于hashcode方法设计的不对呢,还是equals方法设计不对?
: 如果equals方法是 false, 但是hashcode方法返回的是相等。那么应该修改哪一个方法
: 呢?
: 该如何解决这个问题呢?
: 请详细说说,谢谢!

avatar
j*3
32
我也被问到过!!我也不会!
avatar
g*g
33
equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
例子来说hash function实现有问题。

【在 h*******e 的大作中提到】
: “但是equals一样,那么hashcode就肯定一样了“ 这是不见得的,应该看具体怎
: 么定义相等。有可能equals 定义比hash算法严 equals是 hash 的子集,   也有
: 可能 equals和 hash 算法定义的有交集还有差集。

avatar
h*e
34
恩又想了一下这个应该是对的,“equals为 true, hashcode必须相等,否则是bug. 反
之不必须"
后面那半句 hash function 有问题我没说过的,是别的版友说的, 我的equals 的
概念之前不太清。

【在 g*****g 的大作中提到】
: equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
: 例子来说hash function实现有问题。

avatar
y*a
35
没有问题,以下是 hashcode 的 contract:
Whenever it is invoked on the same object more than once during
an execution of a Java application, the {@code hashCode} method
must consistently return the same integer, provided no information
used in {@code equals} comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
If two objects are equal according to the {@code equals(Object)}
method, then calling the {@code hashCode} method on each of
the two objects must produce the same integer result.
It is not required that if two objects are unequal
according to the {@link java.lang.Object#equals(java.lang.Object)}
method, then calling the {@code hashCode} method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.

【在 s********e 的大作中提到】
: 今天面试,被问到HashCode 和equals方法。
: 我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
: 但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
: 处理?
: 这个两个对象还算是相同么?

avatar
s*t
36

同意这个,equals等了,hashcode怎么可能不等啊!! hash同一个key,value能不一样
???这还叫神马hash function啊

【在 g*****g 的大作中提到】
: equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
: 例子来说hash function实现有问题。

avatar
s*e
37
xie xie. Share with others.
http://javarevisited.blogspot.co.uk/2011/02/how-hashmap-works-i
http://javarevisited.blogspot.co.uk/2011/07/java-multi-threadin
http://javarevisited.blogspot.sg/2011/04/top-20-core-java-inter
http://javarevisited.blogspot.sg/2012/02/what-is-race-condition
http://javarevisited.blogspot.co.uk/2011/04/synchronization-in-
http://javarevisited.blogspot.co.uk/2010/10/why-string-is-immut
http://javarevisited.blogspot.co.uk/2011/11/collection-intervie
http://javarevisited.blogspot.sg/2012/06/20-design-pattern-and-
http://javarevisited.blogspot.sg/2011/03/10-interview-questions
http://javarevisited.blogspot.co.uk/2011/11/collection-intervie
http://javarevisited.blogspot.co.uk/2011/04/top-20-core-java-in
Java 多线程下race condition/同步/原子操作问题
http://blog.csdn.net/blade2001/article/details/7490650
http://javarevisited.blogspot.in/2013/03/how-to-create-immutabl
http://javarevisited.blogspot.in/2011/12/final-variable-method-
http://javarevisited.blogspot.sg/2011/10/override-hashcode-in-j
http://javarevisited.blogspot.co.uk/2012/12/inner-class-and-nes
http://javarevisited.blogspot.co.uk/2011/04/garbage-collection-
http://javarevisited.blogspot.co.uk/2011/08/what-is-polymorphis
http://javarevisited.blogspot.co.uk/2011/05/wait-notify-and-not
http://javarevisited.blogspot.co.uk/2010/10/what-is-deadlock-in
http://javarevisited.blogspot.co.uk/2011/10/how-to-stop-thread-
http://javarevisited.blogspot.co.uk/2012/01/difference-thread-v
http://javarevisited.blogspot.sg/2011/11/hotspot-jvm-options-ja
http://javarevisited.blogspot.co.uk/2011/05/java-heap-space-mem
http://javarevisited.blogspot.co.uk/2011/04/synchronization-in-
http://javarevisited.blogspot.sg/2012/03/mixing-static-and-non-
http://javarevisited.blogspot.co.uk/2011/05/java-heap-space-mem
http://javarevisited.blogspot.co.uk/2011/11/decorator-design-pa
http://javarevisited.blogspot.co.uk/2012/07/when-class-loading-
http://javarevisited.blogspot.co.uk/2012/12/how-classloader-wor
http://javarevisited.blogspot.co.uk/2014/07/top-50-java-multith
http://javarevisited.blogspot.co.uk/2013/03/how-to-create-immut
java concurrency
http://www.vogella.com/tutorials/JavaConcurrency/article.html
http://javarevisited.blogspot.co.uk/2014/07/top-50-java-multith
FIX
http://javarevisited.blogspot.co.uk/2011/04/fix-protocol-tutori
http://javarevisited.blogspot.co.uk/2010/12/fix-protocol-interv
JMS MQ
http://javarevisited.blogspot.co.uk/2014/03/top-10-websphere-mq
http://www.javabeat.net/jms-interview-questions/
NIO Socket
http://javarevisited.blogspot.co.uk/2014/08/socket-programming-
Unix for Java developer
http://javarevisited.blogspot.co.uk/2011/05/unix-command-interv
Java并发编程Akka
http://ifeve.com/akka-doc-java-what-is-akka/
Week Reference and Soft Reference
http://javarevisited.blogspot.co.uk/2014/03/difference-between-
Java内部类总结 (吐血之作)
http://blog.csdn.net/hikvision_java_gyh/article/details/8964155
map reduce
http://hadoop.apache.org/docs/r0.19.1/cn/mapred_tutorial.html
eclipse加入源hadoop代码
https://www.youtube.com/watch?v=zuiUEvYTayA
自定义 Mapreduce类
http://datamining.xmu.edu.cn/bbs/home.php?mod=space&uid=91&do=b
http://agiledon.github.io/blog/2013/03/19/hadoop-mapreduce-skil
programming 题:
http://www.java2novice.com/java-interview-programs/
设计模式
http://www.cnblogs.com/seesea125/archive/2012/04/05/2433463.htm
UML
http://www.yongfa365.com/Item/UML-Association-Dependency-Aggreg
谷歌面试准备
http://blog.jobbole.com/18040/
.bat 批处理
http://www.cnblogs.com/SunShineYPH/archive/2011/12/13/2285570.h
局部内部对象引用的局部变量为何必须为final
http://blog.csdn.net/llyzq/article/details/2311271
good mockito
http://www.importnew.com/10083.html
maven
http://jingyan.baidu.com/article/37bce2be153dc91003f3a257.html

wanna

【在 m*****k 的大作中提到】
: this is expected, this is why you need define your own equals() if you wanna
: use the obj as key in HashMap,
: after the same hashcode leads you to the same bucket, you need equals() to
: find the obj that matches your input so to retrieve the right value.
: 放狗, 你一下就明白了。
: 比如
: http://javarevisited.blogspot.com/2013/08/10-equals-and-hashcod
:
: 对?

avatar
T*g
39
这个回答完全不对。
相同hashcode不同equals是非常正常的Hash冲突。这只能说明这两个对象不同。

【在 s*****j 的大作中提到】
: That means equals() is problematic, it will cause unexpected result
: especially in collection operation.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。