Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

HashSet unique values

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about Set. I know that sets do not allow duplicates. I tried the following sample but didn't quite understand how did the JVM automatically understood that I have added d3 object more than once? I have not added any equals() method which would say that two objects are equal based on some instance variable. Please guide.

 
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, you haven't implemented an equals() method. That means that your Dept class inherits its equals() method from Object, and therefore that two Dept objects are "equal" if and only if they are the same object.

But I don't understand your question. Or your code. You're asking about whether the add() method of Set notices when you add an object which is already in the set, but your code doesn't bother to find out. Which it could very easily. Read the documentation for Set's add() method and you will see.
 
Aakash Chandel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:True, you haven't implemented an equals() method. That means that your Dept class inherits its equals() method from Object, and therefore that two Dept objects are "equal" if and only if they are the same object.

But I don't understand your question. Or your code. You're asking about whether the add() method of Set notices when you add an object which is already in the set, but your code doesn't bother to find out. Which it could very easily. Read the documentation for Set's add() method and you will see.



Let me try to make this clear.
The output of the above program is :
I was hoping that the output of the program will have 3-Admin department displayed 3 times since I added it 3 times using set.add(). But that didn't happen.

So, if I try to add two String objects with same values Set won't allow because it uses the String's equals() method to determine if they are same. How does Set figure out if two objects (Dept) are equal and does not add them to Set even though we have not provided equals() method for Dept object?

Hope this makes sense.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though you have not provided equals, its inherited from Object.
 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi try the following you will understand how equals() work...

create another object as follows...


add it into the set


now compiling and running the code you will see this output...
s=[5-Purchase, 2-Finance, 3-Admin, 1-Accounts, 4-HR, 3-Admin]

now override equals() and hashcode() methods...


now compiling and running the code you will see this output...
s=[5-Purchase, 4-HR, 3-Admin, 2-Finance, 1-Accounts]

conclusion...
d3 will always refers to the same object whether you add it several times or not, the compiler will see the same object, this was achieved by this...


but every time you say and giving it a different reference, e.g d6, and Dept does not override equals(), then everytime it's a complete new object, which will never be equal to any other with different reference, until equals() is overriden.

Dept d3 = new Dept(3, "Admin");
Dept d6 = new Dept(3, "Admin");
d3 will always be equal to d3, but d3 will only be equal to d6 if equals() is overriden.

Hope this helps.


 
Aakash Chandel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much better and thanks for the very good explanation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic