[My Blog]
All roads lead to JavaRanch
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Christophe Verré wrote:
the output becomes 2 why??
Before doing this exercise, what did you learn about Maps ? Do you remember how objects are stored in maps ?
Soumya Ranjan Mohanty wrote:i was confused for the size of HashMap, is it only counts the number of discrete objects in a Map after the correct implementation of HashCode ?
[My Blog]
All roads lead to JavaRanch
OCPJP 6.0 93%
OCPJWCD 5.0 98%
mohitkumar gupta wrote:
i think answer should be C and E
hasCode() determines the bucket,so all object would be placed in the same bucket.
Map can hold duplicate entries
and as far i know equals is used so that when we use the
get function to retrieve the data ,the passed object is same as which we
used in the
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Soumya Ranjan Mohanty wrote:
When the hashCode() uncommented , the output becomes 2 why??
Example from Cathy Siera Book.
SCJP6.0,My blog Ranchers from Delhi
phil sohan wrote:
when implemeting hashcode...........please see how hascode works.........
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
first see this will call this method
ToDos(String d) { day = d; }
which will in turn call equals method...........
after equals.....then hascode method will be call...........as it is hascode is commented then it wil call the hashcode method of object class.
which will return some random value........
m.put(t1,"doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
so all these three will be treated as different............
but when we uncomment that.........then hashcode method will return same hascode for 2 Monday and 1 hashcode for Tuesday
So the m.size() will return key value mappings in the map......
which is 2........
but when we are
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP6.0,My blog Ranchers from Delhi
phil sohan wrote:@Abimaran Kugathasan
Could you please telll how hashcode works thenn,,,,,.........
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:
phil sohan wrote:@Abimaran Kugathasan
Could you please telll how hashcode works thenn,,,,,.........
I think, my first reply in this question is correct. Please Have a look on it.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
OCPJP 6.0 93%
OCPJWCD 5.0 98%
mohitkumar gupta wrote:can anyone tell me,whether equals or hascode method would be called when the following line is encountered in the code
i am not able to get ,how the objects t1,t2 are considered same.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
mohitkumar gupta wrote:
3.This is part of the question posted in this thread.
see line 4 of the code
4.The answer given in the book is C and D,but as it doesn't compile,the answer should be A
Abimaran Kugathasan wrote: According to your equals() method, there are two different objects. So the answer is 2. Check by printing the Map.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |