Campbell Ritchie wrote:. You should have learnt that within six months of starting programming Java®.
Campbell Ritchie wrote:. .
Don't know. Were you taught well? Did you take a course or try to teach yourself from books?Monica Shiralkar wrote:. . . . What may be the reason that a developer could not learn that within say first 6 months despite his/her intention to learn ? . . .
Campbell Ritchie wrote:
Don't know. Were you taught well? Did you take a course or try to teach yourself from books?Monica Shiralkar wrote:. . . . What may be the reason that a developer could not learn that within say first 6 months despite his/her intention to learn ? . . .
I hope you meant, “did not learn,” rather than, “could not learn,”.
Campbell Ritchie wrote:Predict what will happen with and without overridden methods and then verify your prediction by running some code.
retrieving emp1->1
Monica Shiralkar wrote:My prediction was: Without overriding hashcode and equals, on trying to use get command to get value using such a key, it will not be able to fetch.
Monica Shiralkar wrote:
If I can retrieve successfully without overriding equals and hashcode then what benefit will I get by overriding ?
Don't expect us to explain the whole story.Dave Tolls wrote:. . . you should be able to work out the problem . . . .
salvin francis wrote:
If the output confuses you further, then I would suggest reading more on how the Map works.
Monica Shiralkar wrote:... what is the need for me to override hashcode?
salvin francis wrote:
Monica Shiralkar wrote:... what is the need for me to override hashcode?
Let's discuss the basics first. Please answer the below question to the best of your knowledge:
What is a hashcode ?
No.Monica Shiralkar wrote:
A unique number associated with every object (since hashcode method is of object class).salvin francis wrote:. . . What is a hashcode ?
That is only a part of the whole story, probably only a small part.Hashing is used by certain collections like Hashset, Hashmap for good retrieval performance.
Dave Tolls wrote:Let's break your code down a bit.
Given the above class, what do I pass into getEmployeeInteger in order to retrieve the associated value in the map for "mary"?
"in order to retrieve the associated value in the map for "mary"
Liutauras Vilda wrote:
Do both hashCode() and equals() methods are always being used when calling myMap.get(...)? Explain your answer.
Monica Shiralkar wrote:In both the cases, I am able to retrieve it (i.e get output as 3) using
System.out.println("Result of get->"+ getEmployeeInteger(emp3));
...
But what I can see by running the code the last post of mine, is that it is happening fine in all the 3 cases below
Monica Shiralkar wrote:...that has been my understanding that we can do this only if the class of which these objects are , overrides hashCode method. But what I can see by running the code the last post of mine, is that it is happening fine in all the 3 cases below...
...Expected result was also the same (since this object was never inserted in the map).
Liutauras Vilda wrote:
At line 8 Joe should figure out how much he won. Please fill in Line 20 so it returns a correct result. Note: program is complete, except Line 20.
Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null.
hashCode for personObj1->2088051243
Enter your name and age to find out how much you won!
hashCode for personObj2->93122545
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null
hashCode for personObj1->100
Enter your name and age to find out how much you won!
hashCode for personObj2->100
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object) is null
Please, you tell us.Monica Shiralkar wrote:. . . Does the error mean that either the equals method was not overridden or it was overridden but not in the correct way?
thanks
Monica Shiralkar wrote:Thanks. In my opinion, the only possibility for this is that equals is not overridden correctly. I will check on how to correct that and try again.
Liutauras Vilda wrote:
Monica Shiralkar wrote:Thanks. In my opinion, the only possibility for this is that equals is not overridden correctly. I will check on how to correct that and try again.
You are trying from the mechanics to understand how the HashMap (in this case) works. I'd first read HOW the hash tables work in general, then you'd understand the WHY about its mechanics.
There are three kinds of actuaries: those who can count, and those who can't.
Note: program is complete, except Line 20.
Piet Souris wrote:There's a way to cheat Liutauras:
Only problem is when that person has become older when queriyng.
Monica Shiralkar wrote:But what exactly is deduced related to what I was seeking?
Liutauras Vilda wrote:...why not O(1) always? Will let you research why, but I already hinted in this post. Due to potential what?
Monica Shiralkar wrote:I got it working.
hashCode for personObj1->100
1. If objects appear to be equal, do their hash codes must be equal too?
2. If hash codes are equal, do objects must be equal too?
salvin francis wrote: You just committed that crime !!
Liutauras Vilda wrote: or just generate implementation with your IDE and look how's done there.
1. If objects appear to be equal, do their hash codes must be equal too?
2. If hash codes are equal, do objects must be equal too?
Consider Paul's rocket mass heater. |