对象值到底指什么?(x.equals(y) == true)应该并不代表对象值相同
class A
{
A(){}
public boolean equals(A a)
{
return true;
}
}
public class EqualsTest
{
public static void main(String argv[])
{
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
System.out.println(a1.hashCode());
System.out.println(a2.hashCode());
}
}
class A
{
A(){}
public boolean equals(A a)
{
return true;
}
}
public class EqualsTest
{
public static void main(String argv[])
{
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
System.out.println(a1.hashCode());
System.out.println(a2.hashCode());
}
}
这里的对象值相同就是指x.equals(y) == true但是这句话反过来说就不对了
两个对象有相同hash code,但却可有不同的值x.equals(y)==false是不对的
建议去看一看数据结构方面的书
这里的对象值相同就是指x.equals(y) == true但是这句话反过来说就不对了
两个对象有相同hash code,但却可有不同的值x.equals(y)==false是不对的
ypdarling(冲动是魔鬼!)
的回答真是一塌糊涂
hashCode也不一样。如果你覆盖了,就看你怎么实现了,所以不定答案。
请问哪里糊涂了?
请问哪里糊涂了?
-------------
hashcode跟数据结构有什么关系?
今天下雨(equals)
和
我没洗衣服一样(hashCode)虽根据常识(推荐的做法)
今天下雨,就最好不要洗衣服(equals -> hashCode == hashCode),但是这个不是强制的,
虽说下雨了就一定没洗衣服阿
hash code 相同,两个对象也可以不同!!强强强强强强啊!!!!!!!!
两个对象有相同hash code,但却可有不同的值x.equals(y)==false是不对的为什么不可以?
事实上是非常可以
今天下雨,就最好不要洗衣服(equals -> hashCode == hashCode)!
不是根据常识,可以这么说吧:equals()是为hashCode不同而设定的!!!
只有使用哈希表是才使用hashCode,equals在任何地方都会用到.
如果你需要使用equals方法或者任何容器,就不要忽略它的存在.
只有你需要使用哈希表时你才需要关注hashCode,而且一般要足够离散!
这句话是对的。我刚才本来是想说:
“两个对象值不同(x.equals(y) == false)时,hash code一定要不同”这句话是不对的。
谁知道刚才一粗心大意没仔细想给打错了,是我不好,sorry。
equals()是为hashCode相同而设定的!!!这句话也可以算是正确。
我认为hashCode离不开equals()
因为当对象的hashCode相同时,哈希表是靠equals()来区分对象的。
两个对象值不同(x.equals(y) == false)时,hash code一定要不同!!!这句话是对的啊,呵呵!经典这句话!!!
我上面明明写了这句话是不对的了,你是什么意思啊?
不是就有面试题这么问的吗?
hashcode跟数据结构有什么关系?不过有一点我还是要坚持,我认为hashcode跟数据结构有很大关系。
去书店随便拿一本数据结构的书,然后翻到查找那一章,你就知道有没有关系了。
请高手来评评哩。
两个对象值不同(x.equals(y) == false)时,hash code可以相同可以不同。
有一定就是错了,不是吗?
你要是有什么问题你就说
要是我错了,我虚心接受,
你别总是把话说得不明不白。
我都不知道你说的是陈述句 还是感叹句,都不知道该怎么理解了。
你别走啊!
打错了!!!应该是equals()是为hashCode相同而设定的!!!equals()离不开hashCode,除非你做没意义的事!!我越来越晕了,equals怎么会是为hashCode而设的,就算没有hashCode,还有equals.
equals又怎么会离不开hashCode呢?
TO ypdarling(冲动是魔鬼!) (
因为当对象的hashCode相同时,哈希表是靠equals()来区分对象的。这句话不准确,哈希表永远是更具equals来区分对象的,但是它有一个基本的规则:如果hashCode不同,就认为他们一定不equals!
TO treeroot(旗鲁特):你要注意我的语气,我坚持那种说法
public int hashCode() {
return (int) System.currentTimeMillis();
}
public boolean equals(Object obj) {
return true;
}
}
TO ypdarling(冲动是魔鬼!) (
因为当对象的hashCode相同时,哈希表是靠equals()来区分对象的。 这句话不准确,哈希表永远是更具equals来区分对象的,但是它有一个基本的规则:如果hashCode 不同,就认为他们一定不equals!我明白你的意思,你也应该明白我的意思,可能是我刚才用词不太严谨,把区分改成查找就应该没问题了。
TO: shine333(enihs) ,从语法角度你可能是指HASHCODE()与TOSTRING()这样才算有强制关系的吧!!但是equals()与hashCode在使用某些容器时确实有有强制性!说他们分不开不过分,容器很多地方用得到!
TO treeroot(旗鲁特):你钻牛角尖了
equals()是为hashCode不同而设定的!!!又是谬论!
只有使用哈希表是才使用hashCode,equals在任何地方都会用到.
如果你需要使用equals方法或者任何容器,就不要忽略它的存在.
只有你需要使用哈希表时你才需要关注hashCode,而且一般要足够离散!
---------------正解.好多时候我们实现equals的时候都可以不用管hashCode.
The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in 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 equals(Object) method, then calling the 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 equals(java.lang.Object) method, then calling the 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 hashtables.
实际上,如果我们的对象并不会放到HashMap中去的话,根本就不需要实现hashCode.是不是这么理解呢?
LinkedHashSet本质上是LinkedHashMap
而不相等的两个对象的hashCode不并不相同,但是如果不相同可以提高hash table的效率。---《Effective Java》 P36
Note that it is generally necessary to override the hashCode method whenever this method is overridden,so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. public boolean equals(Object obj)Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references: It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.