aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() & equals() method test 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 "hashCode() & equals() method test" Watch "hashCode() & equals() method test" New topic
Author

hashCode() & equals() method test

Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Hi Ranchers,

Just I want confirmation on ....
Whan we create any class and I will overrides equals method for comparing class accoring to any variable in that class.


Now as shown in above class we can compare objects of HashSetTest class according to property name of that class.
this is what we get from overriding equals method of Object class in our own class.(we did not override hashCode() method).

Now I am adding objects in HashSet. for that I had overridden both hashCode() & equals() methods.as shown below.


from above example we can say if we override hashCode() method then we should also override equals() method.
As we returning 1 from hashCode() method, for each object of this class it will returns same hashCode i.e. 1,
Now when we add element in HashSet it will check hashcode of object that we going to add to already added elements,
if it is same then it will call equals() on object by passing other object as parameter. In equals we can do custom equal checking as
we checked in frist example. And if it is true then HashSet will not add new Object



Thanks,
Shailesh
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Everything that you said seems fine to me. The only thing that you should remember is that the hashCode method that you've implemented is not very efficient. The hashCode method should be designed in such a way that it returns unequal hashCode for unequal objects. This is also mentioned in the docs
hashCode method API docs wrote:It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi..

i'm wondering about, as we know that with the Java IDE like Netbeans, we can generate hashCode() & equals() method..
is this code generated enough?, or do we've to add additional code?..

sorry for the basic question..

Thanks..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

i'm wondering about, as we know that with the Java IDE like Netbeans, we can generate hashCode() & equals() method..
is this code generated enough?, or do we've to add additional code?..

I don't know what type of code Netbeans generates for the hashCode and equals methods, but I'm sure won't be appropriate in many cases. Netbeans doesn't know what properties you actually want to compare in your class...
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Thanks Ankit Garg for your reply..

This is equals and hashCode code that generated by Netbeans :

Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Ankit, Leonardo Thanks for reply ,

I dont want duplicate object in my HashSet (that means should not have same name property as I overrides equals() method).
for achieving that I was returning 1 from overridden hashCode() method.

How can I achieve uniqueness in HashSet by returning different hashCodes from hashCode() method.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

@Leonardo, the equals method implementation created by Netbeans seems wrong to me. If you look at any of the conditions for example

Now if I call the equals method on a object and pass the same object as parameter (i.e. emp.equals(emp);), then this.id will be equal to other.id, thus the condition this.id != other.id will result in false and the equals method will return false while it should return true.

@Shailesh, in your hashCode method you can return something like return name.length(). This will return different hash code for objects whose name property have different length. This will give better performance with HashSet. We can't return name.hashCode() as you are using equalsIgnoreCase in your equals method...
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Shailesh Narkhede wrote:
How can I achieve uniqueness in HashSet by returning different hashCodes from hashCode() method.


following is code to achieve unique hashCode for unique objects
(Assume that you have a class in which there is instance variable "id" type of int)



SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Leonardo Carreira wrote:Thanks Ankit Garg for your reply..

This is equals and hashCode code that generated by Netbeans :



May be this could help?
it`s generated by eclipse.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode() & equals() method test