...and got no reply. I hope that means you think my questions are so easy that it would be beneath your dignity to answer them. I hope it doesn't mean that you prefer us to give you the answers like some sort of spoon‑feeding. Remember that this forum, programming, and the whole of life have this in common: the more you put into it, the more you will get out.Yesterday, I wrote:And do you understand those implementations? Tell us what differences you may see if . . . .
Campbell Ritchie wrote:[..and got no reply. I hope that means you think my questions are so easy that it would be beneath your dignity to answer them.
Campbell Ritchie wrote: Remember that this forum, programming, and the whole of life have this in common: the more you put into it, the more you will get out.
Campbell Ritchie wrote:And do you understand those implementations
Campbell Ritchie wrote:And do you understand those implementations? Tell us what differences you may see if you useas opposed toor
but you can reduce the multiple returns to one like this
Does it matter which order the subexpressions are evaluated in?
Disagree.Monica Shiralkar wrote:. . . For hashcode, it creates a unique number to return.
AgreeFor equals method it lets us specify that in what case should the objects be considered equal.
You have the checks right, but the order wrong. If you test String equality before type equality, what will happen?Campbell Ritchie wrote:. . . Tell us what differences you may see if you useas opposed toor
First one will will check for the object reference to be same. The second should be used in case of checking if Strings are having the same content. The third one checks if it belongs to that type in the hierarchy.
Tell me which of the five bullet points in the general contract of equals() you are going to violate.
. . . Yes and how about simply using as below: . . .
What would happen if you wrote the subexpressions in a different order?Left to right inside the braces.Does it matter which order the subexpressions are evaluated in?
The following works better:-Earlier, I wrote:
Disagree
You have the checks right, but the order wrong. If you test String equality before type equality, what will happen?
You have the checks right, but the order wrong
I wanted to know the difference between the three lines numbered 16, 16, and 14 in the code above.
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.
What would happen if you wrote the subexpressions in a different order?
No, it is the other way round. The hash code is calculated according to its formula and the Map decides where to locate the corresponding Entry.Monica Shiralkar wrote:. . . same for elements inserted in the same hash buckets. . . . .
Because such a question has come up here before. Because I can remember how a String calculates its hash code.What is the reason why both gave same hashcode? . . .
No.Are you talking about the below code which I had written?
They aren't called braces; they are round brackets or parentheses. Only say “round brackets” in Britain. The extra () are not essential. . . I think in this braces are missing after && and it should be instead like below:. . . .
I meant the lines in the following code.If you were thinking of that code, you are completely wrong.. . .
As per my understanding:
First 16: will check for the object reference to be same.
Second 16: will check if Strings are having the same content.
14: will check if it belongs to that type in the hierarchy.
Correct.Not sure about which one of the below would my code be voilating:
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
Correct
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.
Correct
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.
Incorrect.
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.
Incorrect.For any non-null reference value x, x.equals(null) should return false.
I meant, what if you change this code which I showed you on WednesdaytoThere was a serious error in the original and I have still not corrected it.Some time ago, I wrote:What would happen if you wrote the subexpressions in a different order?
Both AND and OR are associative and commutative, so that is incorrect.As per my understanding that matters in case of OR not AND.
No, it is the other way round. The hash code is calculated according to its formula and the Map decides where to locate the corresponding Entry.
Because I can remember how a String calculates its hash code.
They aren't called braces; they are round brackets or parentheses. Only say “round brackets” in Britain. The extra () are not essential
There was a serious error in the original and I have still not corrected it.
Both AND and OR are associative and commutative, so that is incorrect.
That's a pleasureMonica Shiralkar wrote:Thanks. . . .
Remind yourself of what you wrote.But why is it wrong because I read that 2 keys will be stored in the same bucket if their hash codes are the same,.
The key's hash code is used to decide which “bucket” is used; two hash codes ending in the same bits (after any rehashing) will use the same “bucket”. They do not have to be the same hash codes. It is not a case of the “bucket” determining which hash code goes there. That is why I said it was the other way round.Hashcode has to return an integer which would be same for elements inserted in the same hash buckets.
But I showed two Strings with different content producing the same hash codes, too.. . . . 2 strings with same content return same hashcode. . . .
The code in question might never return a result at all. You should find it easy to find an example which fails to produce a result.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.
No, that isn't the mistake I made. My mistake is much worse. Actually there are no necessary () missing.There was a serious error in the original and I have still not corrected it.
The outside brackets are missing.
You saidBoth AND and OR are associative and commutative, so that is incorrect.
But not sure why was it wrong in that case.
...which is incorrect.that matters in case of OR not AND.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Campbell Ritchie wrote:The key's hash code is used to decide which “bucket” is used; two hash codes ending in the same bits (after any rehashing) will use the same “bucket”. They do not have to be the same hash codes. It is not a case of the “bucket” determining which hash code goes there.
Campbell Ritchie wrote:two hash codes ending in the same bits (after any rehashing) will use the same “bucket”. They do not have to be the same hash codes.
Monica Shiralkar wrote:Yes the object oriented programming. If keys have to be combination of say var1 and var1 for HashMap, I have seen people do things like making var1_var2 as the key instead of creating a class say MyClass with var1 and var2 and then storing objects lile myClassObj1 and so on. For the second one ,one has to override equals and hashcode so some think insteading of doing all that let me simply do var1_var2.But then the second way(creating class ) is the proper way of doing it .
The hard part is choosing the fields, so they are exactly the same as those used in equals(). I think there are some examples in Sierra and Bates' (or Sierra Bates and Robson's) Java8 cert book showing how you can be mistaken about choice of fields for hashCode().Paul Clapham wrote:. . . identify the fields . . . used in the equals() method . . .
Don't understand. How do you work that out? It looks like a combined key to me, which I have only seen in SQL.Monica Shiralkar wrote:. . . making var1_var2 as the key . . .
Campbell Ritchie wrote:
Don't understand. How do you work that out?Monica Shiralkar wrote:. . . making var1_var2 as the key . . .
Campbell Ritchie wrote: two hash codes ending in the same bits (after any rehashing) will use the same “bucket”. They do not have to be the same hash codes. It is not a case of the “bucket” determining which hash code goes there. That is why I said it was the other way round.
Monica Shiralkar wrote:I have a doubt. Is one bucket entirely dedicated to elements of a particular hashcode or the bucket can have values with different hashcodes too?
Paul Clapham wrote:
Monica Shiralkar wrote:I have a doubt. Is one bucket entirely dedicated to elements of a particular hashcode or the bucket can have values with different hashcodes too?
There are more than 4,000,000,000 possible hashcodes.
Monica Shiralkar wrote:So I am trying to understand that based on what criteria is it a particular bucket the address of a particular element.
Monica Shiralkar wrote:Thanks.Yes. Does each bucket have hashcodes of a particular range ?
I am sure it now uses a different rehashing strategy from earlier versions. The structure of the buckets changed from a plain simple linked list to a tree (if n > 8) in Java8.Paul Clapham wrote:. . . HashMap from the standard API, that could have changed from one version to the next.
Monica Shiralkar wrote:Thanks.Yes. Does each bucket have hashcodes of a particular range ?
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
Junilu Lacar wrote:Why do you care? Are you trying to implement a hashmap yourself?
I suggest you research and understand How a HashMap works in Java
Campbell Ritchie wrote:... it says to use a different method if there is only one field.
Monica Shiralkar wrote:Hashing gives better retrieval performance. But this is limited to only some members of Collections framework: HashMap, HashSet, HashTable use hashing. Why not arrayList been given this benifit?
In my eyes, random beginners' tutorial material scattered across the web is jsut as bad. Not all of it. Some of it is good, but the beginners don't know how to distinguish the two yet.Jesse Silverman wrote:. . . one source that seems to have a worse reputation here than random third-party certification-related info scattered across the web, it would probably be random "Java interview question prep" material scattered across the web.
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
|