aspose file tools*
The moose likes Java in General and the fly likes Why we implement hashcode and equals() method  while working with Hashtable and hashMap ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why we implement hashcode and equals() method  while working with Hashtable and hashMap ?" Watch "Why we implement hashcode and equals() method  while working with Hashtable and hashMap ?" New topic
Author

Why we implement hashcode and equals() method while working with Hashtable and hashMap ?

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
Hi Ranchies,


Why we want to implement the hashcode() and equals() method when you dealing with objects in HashTable abd HashMap. That is main Question and could you please
direct me good material on this topic.

Please please

Thansk
Praveen

Francesc Martinez
Greenhorn

Joined: Feb 13, 2009
Posts: 24
Because HashXXX classes uses hashCode() to find objects.

Javadoc in these classes should explain something about that. I also highly recommend the book "Effective Java" for this and other important matters.

You can look for information about hash tables. How they work, how objects are indexed using the "hash code" for quick access.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3394
    
    9
It's good practice to always override hashCode() and equals(Object), even if you're not going to use a HashTable or something similar, as long as it makes sense.

For instance, if two of your objects are always distinct, then you don't have to override these methods, because Object already provides the correct implementation.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
For instance, if two of your objects are always distinct

How the java runtime will understand that the object are distinct or how the jre will tell two user defined objects are equal?
The object class equals only check for references. Say for example for a Employee class objects may be treated equals depending on there dept and experience property not by looking at reference only.


Java Programmer | SCJP 1.5 | SCWCD 1.4
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3394
    
    9
Exactly, and this is why you would override the equals method. A lot of classes use the equals method to see when two objects are distinct or equal. You can override this method to provide your own understanding of when two objects are equal.

Keep in mind that whenever you override equals, you *must* override hashCode as well. Take a good look at the documentation for these methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why we implement hashcode and equals() method while working with Hashtable and hashMap ?
 
Similar Threads
equals() and hashCode()
Same elements in HashMap
why we need to override equals and hascode
hashmap and equals doubts
containsKey() question