由买买提看人间百态

topics

全部话题 - 话题: hashcode
1 2 3 4 5 6 7 8 下页 末页 (共8页)
s*******n
发帖数: 305
1
来自主题: JobHunting版 - Java的hashcode和equal函数有什么用?
刚好我也复习下
equals()反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用
,也可能是值类型的值。
而hashCode()是对象或变量通过哈希算法计算出的哈希值。
之所以有hashCode方法,是因为在批量的对象比较中,hashCode要比equals来得快,很
多集合都用到了hashCode,比如HashTable。
两个obj,如果equals()相等,hashCode()一定相等。
两个obj,如果hashCode()相等,equals()不一定相等(Hash散列值有冲突的情况,虽
然概率很低)。
所以:
可以考虑在集合中,判断两个对象是否相等的规则是:
第一步,如果hashCode()相等,则查看第二步,否则不相等;
第二步,查看equals()是否相等,如果相等,则两obj相等,否则还是不相等。
更多详情: http://www.cnblogs.com/nktblog/articles/2518111.html
f****n
发帖数: 208
2
来自主题: Java版 - 问个Object.hashCode()的问题
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. ... 阅读全帖
S**********C
发帖数: 161
3
来自主题: Java版 - 问个Object.hashCode()的问题
书上说,一般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()返回相同的值?
h*******a
发帖数: 165
4
谢谢!
但是在注释掉最后一行的情况下,我check了t1,t2,t3的hashCode(), 它们返还同样的
数值(都是1743911840),请问这是怎么回事? 当然加上最后一行的情况下,t1,t2,
t3的hashCode()值均是9 (as expected)。
还有就是如果t1.hashCode()不等于t2.hashCode(),但是t1.equals(t2) return true,
这样可以吗?但是我记得是如果t1和t2的hashCode()不同,必然t1.equals(t2) return
false.
还请指教
l****u
发帖数: 1764
5
来自主题: Programming版 - JAVA equals()和hashCode()请教。
没有重载hashCode()的话就默认用Object的hashCode(),这个函数每个对象返回一个不
同的code。你的两个new Turtle(1)的hashCode也是不同的,所以被当做不同的对象可
以共存于一个Set里面了
HashSet判断是否重复的依据是:如果hashCode相同,继续判断equals()是否为true,
true则两个object相同,false则不同;如果hashCode不同,直接跳过equals()函数,
这俩obj不同
s********r
发帖数: 176
6
这个两个对象不相同 if equals returns false
如果两个对象 hashcode值一样,equals方法返回false,那么该如果处理?
需要改进你的hashCode() programming, 尽管equals方法返回false, 两个对象
hashcode值也可以一样, 但不是好的hashCode() implementation,而且会影响的
hashtable的performance,需要改进,
y**********a
发帖数: 824
7
没有问题,以下是 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 ... 阅读全帖
s********r
发帖数: 176
8
这个两个对象不相同 if equals returns false
如果两个对象 hashcode值一样,equals方法返回false,那么该如果处理?
需要改进你的hashCode() programming, 尽管equals方法返回false, 两个对象
hashcode值也可以一样, 但不是好的hashCode() implementation,而且会影响的
hashtable的performance,需要改进,
y**********a
发帖数: 824
9
没有问题,以下是 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 ... 阅读全帖
z*******3
发帖数: 13709
10
i really dont think so
public class Test {
public static void main(String[] args) {
Map m = new HashMap();
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
m.put(t1, "doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println(t3.hashCode());
... 阅读全帖
b****g
发帖数: 192
11
来自主题: JobHunting版 - Java的hashcode和equal函数有什么用?
object class有个hashcode和equals函数。
hashcode()用object的地址算出hashcode
equals()计算两个object是不是相等。
对吗?
这个“相等”是什么意思?
如果面试官问道这两个函数的作用,该怎么回答?
之所以来问,就是因为google出来的答案千奇百怪自相矛盾。
恳请大家不要阴阳怪气的回答。
s********e
发帖数: 340
12
今天面试,被问到HashCode 和equals方法。
我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
处理?
这个两个对象还算是相同么?
s********e
发帖数: 340
13
今天面试,被问到HashCode 和equals方法。
我们知道如果,两个类对象相同,equals 方法返回True,那么Hashcode就应该也一样。
但是,我被问到,如果两个对象 hashcode值一样,equals方法返回false,那么该如果
处理?
这个两个对象还算是相同么?
g*****g
发帖数: 34805
14
来自主题: Java版 - 问个Object.hashCode()的问题
相等是查equals,不要求hashCode返回同样的值。
但是hashCode如果不返回同样的值,在HashMap一类的数据结构里
会出问题。会出现
a.equals(b) == true
hashMap.put(a, blah);
hashMap.get(b) == null;
所以你hashCode返回一个常数都没事,只不过哈希效率极低而已,
但返回不同的值就会出问题。
z*******3
发帖数: 13709
15
会调用object.hashcode
根据虚拟机对象本身的identity来返回hashcode
同一台虚拟机的不同对象自然会返回不同的hashcode
o***i
发帖数: 603
16
因为你写的代码是不符合java“规范”的
java里,这个equals和hashcode是相互联系的,如果t1.equals(t2) true,那么t1和t2的
hashcode()必须也相等的
If two objects are equal according to the equals(Object) method, then callin
g the hashCode method on each of the two objects must produce the same integ
er result.
这就是为什么这两个method要同时override

key
h*******a
发帖数: 165
17
谢谢,但是在这种不符合规范的情况下我想弄清楚到底是equals()还是hashCode()决定
了t1,t2是否是相同的Map key.
我的理解是,如果t1,t2的hashCode()的值是不同的,程序就会认为t1,t2就是不同的
key(当然这可能不符合规范),这时就不再用equals()来判定了。如果t1,t2的
hashCode()值是相同的,再用t1.equals(t2)来判定,如果ture, t1,t2就是相同的key
,如果false, t1,t2就是不同的key。
不知道我的理解是否正确?

t2的
callin
integ
r**e
发帖数: 226
18
来自主题: JobHunting版 - 问个java hashcode的题
为什么答案是 3? 不是2么?
package test;
import java.util.HashSet;
public class Test {

private String str;

public Test(String str) {
this.str = str;
}

@Override
public int hashCode() {
int hascode = this.str.hashCode();
return hascode;
}

@Override
public boolean equals(Object obj) {
return this.str.equals(obj);
}

public static void main(String args[]) {
Test h1 = new Test("1");
Test h2 = new Tes... 阅读全帖
w*********m
发帖数: 4740
19
来自主题: JobHunting版 - Java的hashcode和equal函数有什么用?
hashmap/hashtable/hashset 用hashcode, treemap/treeset用equals
经典面试题是,如果override的equals,还要考虑override哪个function,就是
hashcode
s********e
发帖数: 340
20
那么请问对于这种情况,是属于hashcode方法设计的不对呢,还是equals方法设计不对?
如果equals方法是 false, 但是hashcode方法返回的是相等。那么应该修改哪一个方法
呢?
该如何解决这个问题呢?
请详细说说,谢谢!
w****r
发帖数: 15252
21
没有问题啊,hashcode可以一样,而equals不一样,概率很低,但是还是有可能有
但是equals一样,那么hashcode就肯定一样了
要不然就是写方法的那个家伙有问题
m*****k
发帖数: 731
22
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
发帖数: 340
23
那么请问对于这种情况,是属于hashcode方法设计的不对呢,还是equals方法设计不对?
如果equals方法是 false, 但是hashcode方法返回的是相等。那么应该修改哪一个方法
呢?
该如何解决这个问题呢?
请详细说说,谢谢!
w****r
发帖数: 15252
24
没有问题啊,hashcode可以一样,而equals不一样,概率很低,但是还是有可能有
但是equals一样,那么hashcode就肯定一样了
要不然就是写方法的那个家伙有问题
m*****k
发帖数: 731
25
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

对?
c*******n
发帖数: 442
26
来自主题: JobHunting版 - hashcode面试题
按区间划分?
比如 > 3 sigma的都算成一个hashcode
http://mathworld.wolfram.com/NormalDistribution.html
或者另一种说法就是对正态分布P(x)积分得到D(x),然后对y轴等间距取区间,对应到x
轴的区间,同一个区间内的算成一个hashcode
F****n
发帖数: 3271
27
来自主题: Java版 - 问个Object.hashCode()的问题
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
发帖数: 161
28
来自主题: Java版 - 问个Object.hashCode()的问题
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?
S**********C
发帖数: 161
29
来自主题: Java版 - 问个Object.hashCode()的问题
我知道这个是个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 con... 阅读全帖
x*****p
发帖数: 1707
30
来自主题: Java版 - 问个Object.hashCode()的问题
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)
h*******a
发帖数: 165
31
代码如下:
import java.util.*;
class MapEQ {
public static void main(String[] args) {
Map m = new HashMap();
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
m.put(t1, "doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
System.out.println(m.size());
} }
class ToDos{
String day;
ToDos(String d) { day = d; }
public boolean equals(Object o) {
return ((ToDos)o).day == this.day;
}
//public int hashCode() { return 9; }
... 阅读全帖
h*******a
发帖数: 165
32
谢谢,是我弄错了。但是第二个问题是,在最后一行被注释掉的情况下,t1和t2的
hashCode()不同,但是有t1.equals(t2) return true, 这样岂不是Map里面有两个key
是相同的了?还是说只要t1和t2是指向两个不同的object,这样就算两个不同的key,
而不是由equals method 所决定的?
然而在最后一行加入的情况下,t1,t2的hashCode()相同(=9),并且t1.equals(t2)
return true,程序判定t1和t2是相同的key,这符合是equals决定t1,t2是否为相同的
key
似乎两种情况有些矛盾?
c*********e
发帖数: 16335
33
你自己把equals(),hashcode()override了,那能怪谁?
public boolean equals(Object o) {
return ((ToDos)o).day == this.day;
}
public int hashCode() { return 9; }

t1
v******y
发帖数: 84
34
来自主题: Programming版 - 怎么覆写hashcode和equals比较好?
只要hashCode 和equals是一致就行了,equal的就一定是一样的hashcode
咋样搞是算法问题,有没有常量不会提高hash质量
equal还是自己写,hascode用Objects.hash(name,age)就行了
l*******n
发帖数: 35
35
来自主题: Programming版 - hashcode面试题 (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: lfranklin (zzz), 信区: JobHunting
标 题: hashcode面试题
发信站: BBS 未名空间站 (Fri Nov 6 10:11:16 2015, 美东)
一道面试题
input key是正态分布,问怎么设计hashcode 让mapping出来的结果比较均匀?
w****6
发帖数: 796
36
来自主题: Programming版 - JAVA equals()和hashCode()请教。
if hashset:
两步:
1)hashCode(), then
2)equals()
建议都一下<>:
hashCode() and equals() 必须同时 override and in sync
if treeset:
参见 HoboCS (Kane)
h*******e
发帖数: 1377
37
“但是equals一样,那么hashcode就肯定一样了“ 这是不见得的,应该看具体怎
么定义相等。有可能equals 定义比hash算法严 equals是 hash 的子集,   也有
可能 equals和 hash 算法定义的有交集还有差集。
g*****g
发帖数: 34805
38
equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
例子来说hash function实现有问题。
h*******e
发帖数: 1377
39
恩又想了一下这个应该是对的,“equals为 true, hashcode必须相等,否则是bug. 反
之不必须"
后面那半句 hash function 有问题我没说过的,是别的版友说的, 我的equals 的
概念之前不太清。
s******t
发帖数: 229
40

同意这个,equals等了,hashcode怎么可能不等啊!! hash同一个key,value能不一样
???这还叫神马hash function啊
h*******e
发帖数: 1377
41
“但是equals一样,那么hashcode就肯定一样了“ 这是不见得的,应该看具体怎
么定义相等。有可能equals 定义比hash算法严 equals是 hash 的子集,   也有
可能 equals和 hash 算法定义的有交集还有差集。
g*****g
发帖数: 34805
42
equals为 true, hashcode必须相等,否则是bug. 反之不必须。没有统计也不能以一个
例子来说hash function实现有问题。
h*******e
发帖数: 1377
43
恩又想了一下这个应该是对的,“equals为 true, hashcode必须相等,否则是bug. 反
之不必须"
后面那半句 hash function 有问题我没说过的,是别的版友说的, 我的equals 的
概念之前不太清。
s******t
发帖数: 229
44

同意这个,equals等了,hashcode怎么可能不等啊!! hash同一个key,value能不一样
???这还叫神马hash function啊
s********e
发帖数: 340
T*********g
发帖数: 496
46
这个回答完全不对。
相同hashcode不同equals是非常正常的Hash冲突。这只能说明这两个对象不同。
l*******n
发帖数: 35
47
来自主题: JobHunting版 - hashcode面试题
一道面试题
input key是正态分布,问怎么设计hashcode 让mapping出来的结果比较均匀?
x*****a
发帖数: 610
48
来自主题: JobHunting版 - hashcode面试题
这是一道概率题呀
把key带入到normal distribution的CDF(Cumulative distribution function)中,
得到的就是均匀0,1分布,之后再正常的rescale一下取模算hashcode就行了
g*****g
发帖数: 34805
49
for hibernate?
if i check a.equals(b) in a hibernate session, if they have the same id,
the equation should always be true, right? Because the same object will not
be loaded twice in hibernate.
So I don't need to write equals and hashcode for domain object,
am I correct?
S**********C
发帖数: 161
50
来自主题: Java版 - 问个Object.hashCode()的问题
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?

retur... 阅读全帖
1 2 3 4 5 6 7 8 下页 末页 (共8页)