File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Collections" Watch "Collections" New topic


maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
i am really confused about entire collections..................
somebody tell me whether when should i use equals and hashCode method......and when should i use Comparable and Comparator???
i know that when we override equals but not hashCode our duplicates are not removed and when we override our duplicate elements are removed???


Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183

first, when you override equals, always override hashCode. Those two go alongside and all contracts involve them both.

Now here is how it works.

Two instances of the same type are always different (the == operator returns false) since what is compared is the object's reference.

Now, there has to be a way to declare two objects as 'meaningfully equivalent'. Imagine the Date class. two instances should be the same when their date is the same. This is where you would override the equals method and do the comparison.

hashCode gives an instance something like an ID. two instances can have the same hashCode but still be different. But the same does not apply vice versa.

Collections use hashCode to allocate the spot where the object instances gets stored.
Imagine a Set. If you add an object to the set, it's hashCode method is invoked, an ID comes back that tells the Set where to put the object. Now another object comes along with a different ID, which gets stored in another location. Now an object with the same ID comes along. The set finds that there already is an object in that spot. If now object1.equals(object2) returns true, the new object will not be addded, if it returns false it will be added.
This is where two different objects can have the same hashCode becomes important.
Also, you see why the hashCode method becomes important. If hashCode is not implemented properly, the Set would always allocate different objects in different spots, hence one spot contains one object and there is no way to check for equality which means that duplicates will result.

Why the whole mess of hashCodes and different spots where objects go?
Imagine you put 458943058 objects into your set. They'll be nicely distributed between a number if spots so that if you want to find a specific object, this can happen fast :-)

Hope this helps

JDBCSupport - An easy to use, light-weight JDBC framework -
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
hey thanks was great!!!
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
hey i have tried using different classes which implement Collection and Map...
and i have come to a conclusion that Treemap,TreeSet and alll Lists like ArrayList,LinkedList and Vector need Comparable or Comparator to sort sequences........and others like HashSet,LinkedHashMap,HashMap,LinkedHashMap,Hashtable they all need to override equals and hashcode to remove duplicate i right...this is what was happening in my programs......incase if i am wrong please correct me.....

I agree. Here's the link:
subject: Collections
jQuery in Action, 3rd edition