Why there is other situation when key's hashcode has three situation for greater than or less than or equals...
There is some code in java.util.HashMap.TreeNode#putTreeVal()
, just like below:
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
There have two situation: h
less than ph
, h
greater than ph
.
Usually, the code (pk = p.key) == k || (k != null && k.equals(pk))
means h
equals to ph
, but i don't know why there still else if after that.
What is the situation when two objects's hashCode
is equals to each other, but ==
and euqlas()
will get false?
When Object's class override equals()
method will cause this situation? But i used heard that override equals()
must override hashCode()
too, so this question wouldn't happen to.
I hope some people could tell me which situation will cause the third else if
.
java hashmap
add a comment |
There is some code in java.util.HashMap.TreeNode#putTreeVal()
, just like below:
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
There have two situation: h
less than ph
, h
greater than ph
.
Usually, the code (pk = p.key) == k || (k != null && k.equals(pk))
means h
equals to ph
, but i don't know why there still else if after that.
What is the situation when two objects's hashCode
is equals to each other, but ==
and euqlas()
will get false?
When Object's class override equals()
method will cause this situation? But i used heard that override equals()
must override hashCode()
too, so this question wouldn't happen to.
I hope some people could tell me which situation will cause the third else if
.
java hashmap
3
"What is the situation when two objects's hashCode is equals to each other, but == and euqlas() will get false?" - any time there's a hash collision. Two objects having the same hash code means they might be equal, not that they are equal. There are only 2^32 possible values fromhashCode()
, but far more than 2^32 possible strings, for example - so there must be strings that have the same hash code, but aren't equal.
– Jon Skeet
Jan 19 at 9:14
From the documentation: "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 hash tables."
– Jon Skeet
Jan 19 at 9:15
Are two Java objects with same hashcodes not necessarily equal? - Stack Overflow
– user202729
Jan 19 at 9:16
Thank you @JonSkeet, you got the point. I ignored hash collision, i knew that different objects may have same hashCode but didn't have profound understanding and apply it to HashMap.
– shengbang he
Jan 19 at 14:18
add a comment |
There is some code in java.util.HashMap.TreeNode#putTreeVal()
, just like below:
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
There have two situation: h
less than ph
, h
greater than ph
.
Usually, the code (pk = p.key) == k || (k != null && k.equals(pk))
means h
equals to ph
, but i don't know why there still else if after that.
What is the situation when two objects's hashCode
is equals to each other, but ==
and euqlas()
will get false?
When Object's class override equals()
method will cause this situation? But i used heard that override equals()
must override hashCode()
too, so this question wouldn't happen to.
I hope some people could tell me which situation will cause the third else if
.
java hashmap
There is some code in java.util.HashMap.TreeNode#putTreeVal()
, just like below:
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
There have two situation: h
less than ph
, h
greater than ph
.
Usually, the code (pk = p.key) == k || (k != null && k.equals(pk))
means h
equals to ph
, but i don't know why there still else if after that.
What is the situation when two objects's hashCode
is equals to each other, but ==
and euqlas()
will get false?
When Object's class override equals()
method will cause this situation? But i used heard that override equals()
must override hashCode()
too, so this question wouldn't happen to.
I hope some people could tell me which situation will cause the third else if
.
java hashmap
java hashmap
edited Jan 19 at 9:13
Jon Skeet
1083k68579178424
1083k68579178424
asked Jan 19 at 9:10
shengbang heshengbang he
436
436
3
"What is the situation when two objects's hashCode is equals to each other, but == and euqlas() will get false?" - any time there's a hash collision. Two objects having the same hash code means they might be equal, not that they are equal. There are only 2^32 possible values fromhashCode()
, but far more than 2^32 possible strings, for example - so there must be strings that have the same hash code, but aren't equal.
– Jon Skeet
Jan 19 at 9:14
From the documentation: "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 hash tables."
– Jon Skeet
Jan 19 at 9:15
Are two Java objects with same hashcodes not necessarily equal? - Stack Overflow
– user202729
Jan 19 at 9:16
Thank you @JonSkeet, you got the point. I ignored hash collision, i knew that different objects may have same hashCode but didn't have profound understanding and apply it to HashMap.
– shengbang he
Jan 19 at 14:18
add a comment |
3
"What is the situation when two objects's hashCode is equals to each other, but == and euqlas() will get false?" - any time there's a hash collision. Two objects having the same hash code means they might be equal, not that they are equal. There are only 2^32 possible values fromhashCode()
, but far more than 2^32 possible strings, for example - so there must be strings that have the same hash code, but aren't equal.
– Jon Skeet
Jan 19 at 9:14
From the documentation: "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 hash tables."
– Jon Skeet
Jan 19 at 9:15
Are two Java objects with same hashcodes not necessarily equal? - Stack Overflow
– user202729
Jan 19 at 9:16
Thank you @JonSkeet, you got the point. I ignored hash collision, i knew that different objects may have same hashCode but didn't have profound understanding and apply it to HashMap.
– shengbang he
Jan 19 at 14:18
3
3
"What is the situation when two objects's hashCode is equals to each other, but == and euqlas() will get false?" - any time there's a hash collision. Two objects having the same hash code means they might be equal, not that they are equal. There are only 2^32 possible values from
hashCode()
, but far more than 2^32 possible strings, for example - so there must be strings that have the same hash code, but aren't equal.– Jon Skeet
Jan 19 at 9:14
"What is the situation when two objects's hashCode is equals to each other, but == and euqlas() will get false?" - any time there's a hash collision. Two objects having the same hash code means they might be equal, not that they are equal. There are only 2^32 possible values from
hashCode()
, but far more than 2^32 possible strings, for example - so there must be strings that have the same hash code, but aren't equal.– Jon Skeet
Jan 19 at 9:14
From the documentation: "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 hash tables."
– Jon Skeet
Jan 19 at 9:15
From the documentation: "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 hash tables."
– Jon Skeet
Jan 19 at 9:15
Are two Java objects with same hashcodes not necessarily equal? - Stack Overflow
– user202729
Jan 19 at 9:16
Are two Java objects with same hashcodes not necessarily equal? - Stack Overflow
– user202729
Jan 19 at 9:16
Thank you @JonSkeet, you got the point. I ignored hash collision, i knew that different objects may have same hashCode but didn't have profound understanding and apply it to HashMap.
– shengbang he
Jan 19 at 14:18
Thank you @JonSkeet, you got the point. I ignored hash collision, i knew that different objects may have same hashCode but didn't have profound understanding and apply it to HashMap.
– shengbang he
Jan 19 at 14:18
add a comment |
2 Answers
2
active
oldest
votes
What is the situation when two objects's hashCode is equals to each
other, but == and equals() will get false?
According to Java Documentation:
- If objects are equal (i.e.
x.equals(y) == true
), thenhashCode
of these object should also be equal (i.e.x.hashCode() == y.hashCode()
) - If two objects with equal
hashCode
(i.e.x.hashCode() == y.hashCode()
), then there's not mandatory for these objects to be equal (i.e.x.equals(y) == true/false
)
See details in Oracle Java Tutorial: Object as a Superclass
add a comment |
What is the situation when two objects's hashCode is equals to each other, but == and equals() will get false ?
When a hashcode collision occurs.
Example
Consider those two Longs:
Long l1 = 1L;
Long l2 = 4294967296L; //which is 2 ^ 32
Do you agree that there are different and equals()
will return false? However, the result of
l1.hashCode() == l2.hashCode()
is true.
Why? Look at the implementation of hashCode for Long
:
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
Since a long can have 2^64 values and the return value of hashcode is a int which can have 2^32 values, it is normal that you have collisions (each value makes collisions with 2^32 other values).
Clarifications
But i used heard that override equals() must override hashCode() too,
so this question wouldn't happen to.
Yes, when you override equals()
you should override hashCode()
too. This is true but I think you mixed up the implications. From the javadoc of hashcode:
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
hash tables.
So the implication
a.equals(b) => a.hashCode() == b.hashCode()
has to be always true (if your methods are correctly implemented) but the converse implication
a.hashCode() == b.hashCode() => a.equals(b)
doesn't necessarily have to.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265589%2fwhy-there-is-other-situation-when-keys-hashcode-has-three-situation-for-greater%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What is the situation when two objects's hashCode is equals to each
other, but == and equals() will get false?
According to Java Documentation:
- If objects are equal (i.e.
x.equals(y) == true
), thenhashCode
of these object should also be equal (i.e.x.hashCode() == y.hashCode()
) - If two objects with equal
hashCode
(i.e.x.hashCode() == y.hashCode()
), then there's not mandatory for these objects to be equal (i.e.x.equals(y) == true/false
)
See details in Oracle Java Tutorial: Object as a Superclass
add a comment |
What is the situation when two objects's hashCode is equals to each
other, but == and equals() will get false?
According to Java Documentation:
- If objects are equal (i.e.
x.equals(y) == true
), thenhashCode
of these object should also be equal (i.e.x.hashCode() == y.hashCode()
) - If two objects with equal
hashCode
(i.e.x.hashCode() == y.hashCode()
), then there's not mandatory for these objects to be equal (i.e.x.equals(y) == true/false
)
See details in Oracle Java Tutorial: Object as a Superclass
add a comment |
What is the situation when two objects's hashCode is equals to each
other, but == and equals() will get false?
According to Java Documentation:
- If objects are equal (i.e.
x.equals(y) == true
), thenhashCode
of these object should also be equal (i.e.x.hashCode() == y.hashCode()
) - If two objects with equal
hashCode
(i.e.x.hashCode() == y.hashCode()
), then there's not mandatory for these objects to be equal (i.e.x.equals(y) == true/false
)
See details in Oracle Java Tutorial: Object as a Superclass
What is the situation when two objects's hashCode is equals to each
other, but == and equals() will get false?
According to Java Documentation:
- If objects are equal (i.e.
x.equals(y) == true
), thenhashCode
of these object should also be equal (i.e.x.hashCode() == y.hashCode()
) - If two objects with equal
hashCode
(i.e.x.hashCode() == y.hashCode()
), then there's not mandatory for these objects to be equal (i.e.x.equals(y) == true/false
)
See details in Oracle Java Tutorial: Object as a Superclass
edited Jan 19 at 9:50
PranshuKhandal
1559
1559
answered Jan 19 at 9:23
oleg.cherednikoleg.cherednik
6,43621118
6,43621118
add a comment |
add a comment |
What is the situation when two objects's hashCode is equals to each other, but == and equals() will get false ?
When a hashcode collision occurs.
Example
Consider those two Longs:
Long l1 = 1L;
Long l2 = 4294967296L; //which is 2 ^ 32
Do you agree that there are different and equals()
will return false? However, the result of
l1.hashCode() == l2.hashCode()
is true.
Why? Look at the implementation of hashCode for Long
:
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
Since a long can have 2^64 values and the return value of hashcode is a int which can have 2^32 values, it is normal that you have collisions (each value makes collisions with 2^32 other values).
Clarifications
But i used heard that override equals() must override hashCode() too,
so this question wouldn't happen to.
Yes, when you override equals()
you should override hashCode()
too. This is true but I think you mixed up the implications. From the javadoc of hashcode:
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
hash tables.
So the implication
a.equals(b) => a.hashCode() == b.hashCode()
has to be always true (if your methods are correctly implemented) but the converse implication
a.hashCode() == b.hashCode() => a.equals(b)
doesn't necessarily have to.
add a comment |
What is the situation when two objects's hashCode is equals to each other, but == and equals() will get false ?
When a hashcode collision occurs.
Example
Consider those two Longs:
Long l1 = 1L;
Long l2 = 4294967296L; //which is 2 ^ 32
Do you agree that there are different and equals()
will return false? However, the result of
l1.hashCode() == l2.hashCode()
is true.
Why? Look at the implementation of hashCode for Long
:
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
Since a long can have 2^64 values and the return value of hashcode is a int which can have 2^32 values, it is normal that you have collisions (each value makes collisions with 2^32 other values).
Clarifications
But i used heard that override equals() must override hashCode() too,
so this question wouldn't happen to.
Yes, when you override equals()
you should override hashCode()
too. This is true but I think you mixed up the implications. From the javadoc of hashcode:
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
hash tables.
So the implication
a.equals(b) => a.hashCode() == b.hashCode()
has to be always true (if your methods are correctly implemented) but the converse implication
a.hashCode() == b.hashCode() => a.equals(b)
doesn't necessarily have to.
add a comment |
What is the situation when two objects's hashCode is equals to each other, but == and equals() will get false ?
When a hashcode collision occurs.
Example
Consider those two Longs:
Long l1 = 1L;
Long l2 = 4294967296L; //which is 2 ^ 32
Do you agree that there are different and equals()
will return false? However, the result of
l1.hashCode() == l2.hashCode()
is true.
Why? Look at the implementation of hashCode for Long
:
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
Since a long can have 2^64 values and the return value of hashcode is a int which can have 2^32 values, it is normal that you have collisions (each value makes collisions with 2^32 other values).
Clarifications
But i used heard that override equals() must override hashCode() too,
so this question wouldn't happen to.
Yes, when you override equals()
you should override hashCode()
too. This is true but I think you mixed up the implications. From the javadoc of hashcode:
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
hash tables.
So the implication
a.equals(b) => a.hashCode() == b.hashCode()
has to be always true (if your methods are correctly implemented) but the converse implication
a.hashCode() == b.hashCode() => a.equals(b)
doesn't necessarily have to.
What is the situation when two objects's hashCode is equals to each other, but == and equals() will get false ?
When a hashcode collision occurs.
Example
Consider those two Longs:
Long l1 = 1L;
Long l2 = 4294967296L; //which is 2 ^ 32
Do you agree that there are different and equals()
will return false? However, the result of
l1.hashCode() == l2.hashCode()
is true.
Why? Look at the implementation of hashCode for Long
:
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
Since a long can have 2^64 values and the return value of hashcode is a int which can have 2^32 values, it is normal that you have collisions (each value makes collisions with 2^32 other values).
Clarifications
But i used heard that override equals() must override hashCode() too,
so this question wouldn't happen to.
Yes, when you override equals()
you should override hashCode()
too. This is true but I think you mixed up the implications. From the javadoc of hashcode:
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
hash tables.
So the implication
a.equals(b) => a.hashCode() == b.hashCode()
has to be always true (if your methods are correctly implemented) but the converse implication
a.hashCode() == b.hashCode() => a.equals(b)
doesn't necessarily have to.
edited Jan 19 at 9:46
answered Jan 19 at 9:18
RicolaRicola
1,325314
1,325314
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265589%2fwhy-there-is-other-situation-when-keys-hashcode-has-three-situation-for-greater%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
"What is the situation when two objects's hashCode is equals to each other, but == and euqlas() will get false?" - any time there's a hash collision. Two objects having the same hash code means they might be equal, not that they are equal. There are only 2^32 possible values from
hashCode()
, but far more than 2^32 possible strings, for example - so there must be strings that have the same hash code, but aren't equal.– Jon Skeet
Jan 19 at 9:14
From the documentation: "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 hash tables."
– Jon Skeet
Jan 19 at 9:15
Are two Java objects with same hashcodes not necessarily equal? - Stack Overflow
– user202729
Jan 19 at 9:16
Thank you @JonSkeet, you got the point. I ignored hash collision, i knew that different objects may have same hashCode but didn't have profound understanding and apply it to HashMap.
– shengbang he
Jan 19 at 14:18