Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Using java.util.HashSet for Custom Class

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to use java.util.HashSet for storing collection of non-duplicate values of my own custom class.

Here is the sample program which I tried.




Output:

hashcode called97
hashcode called98
hashcode called97
hashcode called98
hashcode called97
s = [a, a, a, 1, b, b]

The UsingSet class has a single field value and two UsingSet instances are equal if their values are equal.

I dont know how add() method works to compare objects. But from the above output the add() method is not using the overloaded equals method (the print statements are getting not printed) and it adds to set even if attribute value has the same value.

Overloading hashCode() method also doesn't seems to work.

I tried the same with java.util.TreeSet() by passing my own comparator to constructor and it works fine.

Is it not possible with HashSet()..?

But again, trying the same for Integer class (look at last two add statements and output) works fine and Integer instances considered as equal if their values are equal..

So I think this should be possible..

Can you please help me if anyone know how to implement this.

Thanks very much,

Raja.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajasekar,

Your signature of equals is wrong. It should be:


In your code you do not override but overload, therefore
the old good Object.equals is called.

P.
[ October 06, 2004: Message edited by: Petr Blahos ]
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Petr Blahos

Yes It works now.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am facing a similar problem. The equals method seems to have been defined correctly
I am trying to use a custom class for HashSet. I have overridden the equals and hashcode method.

Observations
1. if I use Hashset, both the employees get added. However, e1.equals(e2) returns true.
2. If I use TreeSet, Only 1 object gets added.

While debugging, Employee -> equals method doesn't seem to get called from HashSet -> add method

What mistake am I making here ?








 
Sheriff
Posts: 22575
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't kick a 4 year old thread.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your hashCode() method is based on the name, but equals is based on the id. The behavior of the two methods is therefore inconsistent. In particular, the two objects you're testing with are equals(), but they have different hashCode() values. The HashSet will only call equals() to check for the equality of two objects with the same hashCode().
 
Marshal
Posts: 74727
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also your equals() method does not test whether the two Objects are of the same class; you can suffer a ClassCastException.

Google for Angelika Langer Java equals method and you will find some useful information there.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also your equals() method does not test whether the two Objects are of the same class; you can suffer a ClassCastException.



equals() does indeed, although compareTo() does not.
 
Tushar Mishra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest / Campbell,

Thanks for your quick reply.

I changed the hashcode() method and made it consistent with equals() method as below. I am getting the expected results now. Thanks for clarifying the concept regarding equals() and hashcode() methods.



I am sorry for the late reply.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic