• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode() & equals() method test

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit Garg for your reply..

This is equals and hashCode code that generated by Netbeans :

 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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...
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


reply
    Bookmark Topic Watch Topic
  • New Topic