This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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
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.
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, 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...