Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes .equals()-Regarding collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ".equals()-Regarding collections" Watch ".equals()-Regarding collections" New topic

.equals()-Regarding collections

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
From the K&B 1.5 p. 526-27

Of Course,overriding the equals() for Car also allows the potential that more than one object representing a single unique car can exist, which might not be safe in your design. Fortunately the String and Wrapper classes work well as keys in hashtable-they override the equals() method.

I dont quite understand the above. What does the overriden equals() of String and wrapper class has so special about it that I couldnt do with my class car. If I override the equals() of car class how is that not as good as the overiden equals() of String.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

First Check why we need to override hashcode and why we need to override equals method.
Check how hashcode and equals are interlinked with each other.
Check proper functions of map.

Then in above code comment and uncomment hashcode or equals method
and see result carefully.

If you will understand above code then you will be able to answer any question in exam regarding hashcode and equals method.
I agree. Here's the link:
subject: .equals()-Regarding collections
It's not a secret anymore!