rama rajesh wrote:i heard that answer override hashcode and equals method. but i dont know how to override these methods.
please provide me the code.
In Collections, the items are put into buckets.
The hashCode determines which bucket item goes into.
Set doesn't allow duplicate items.
So, if there is an item already in the bucket and you try to add new item with same hashCode, the new item is discarded.
I'm giving the code below, but understand before using it.
Add the following member functions to the A class
Just read =>
DontBeACodeMill <= and =>
LetThemDoTheirOwnHomework <=
The hashCode() returns the unique int value for an item.
Two equal items have same hashCode, so you can say multiply variables x, y, z
Choose the hashCode function efficiently.
If you multiply x, y, z in the function, objects [10 20 30] and [20 10 30] may be counted as equal, which you don't want.
Your implementation of equals() method must satisfy the properties of equivalence relation (Reflexive, Symmetric, Transitive, Consistent and Null Comparison)