~ Mansukh
---------------------------------<br />S.C.J.P 1.4(86%),S.C.W.C.D 1.4(88%)<br /> <br />God Must love crazy people...<br />He Makes so many of them.
Jesper Young wrote:The program doesn't show what's in the map, it only shows what you're putting in.
Jesper Young wrote:But in this case, even if you did override equals() and hashCode() in the Person class correctly, you would still get the same output.
The program doesn't show what's in the map, it only shows what you're putting in. You can add two objects that are equal twice into a Set; you will not get an exception or anything like that when you do that. What will happen is that the second object will replace the first object that was already in the map.
~ Mansukh
Naresh Shanmugam wrote:You didn't override hashcode and equals method.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Mansukhdeep Thind wrote:Hi Jesper
I ran the following code to check what are the contents of the set:
...
I got the output :
Jane
Lotte
Jane
Hans
Hans
Your claim was that if I try inserting duplicate elements, the second one would replace the first one. But here, both "Jane" and "Hans" are present twice over. Please explain.
Regards
Mansukhdeep
SCJP6.0,My blog Ranchers from Delhi
shanky sohar wrote:how the hashset detect that the object,value ,string,integer whatever is going to add into it..........is a duplicate........already exist.............
by implements hashcode and equals methods..............
the implementation of both is necessary..............because equals methods will check whether two objects are equals or not....
it will be able to determine if those objects are put in a same hash bucket............
you can override only a equals method but that even doesnot work because only the combination of both will determine the duplicate value...
Remember String,integer,float,long implicitly implemented a hashcode and equals methods................
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
James Sabre wrote:
shanky sohar wrote:how the hashset detect that the object,value ,string,integer whatever is going to add into it..........is a duplicate........already exist.............
by implements hashcode and equals methods..............
the implementation of both is necessary..............because equals methods will check whether two objects are equals or not....
it will be able to determine if those objects are put in a same hash bucket............
you can override only a equals method but that even doesnot work because only the combination of both will determine the duplicate value...
Remember String,integer,float,long implicitly implemented a hashcode and equals methods................
I wish I could find out which language starts (most) sentences with a small letter and ends sentences with multiple full stops. It just makes it very very difficult for people like me who expect a capital letter to start a sentence and a single full stop (or exclamation or question mark) to end a sentence.
SCJP6.0,My blog Ranchers from Delhi
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|