Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedHashSet

 
swapna mallipudi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Question is from CertPal. What is the output of the program?


Size is 5. I don't know what I am missing here. I was thinking LinkedHashSet doesn't allow duplicates. Then howcome the size is 5.

Thank you.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking LinkedHashSet doesn't allow duplicates.

How do you know if a Lion is equal to another ?
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case of set it always first check the HashCode and the use the equals to compare the objects.
Lion class does not override the hashcode and equals method so two objects of lion class will never be equal until they are not pointing to same object. So there will not be any duplicate lion object but same is not true for String objects.

If you try the below code :


Output will be


See, there are 3 lion objects and 2 String objects Because String override equals method but Lion does not. If you override the equals and hashcode methods in Lion class you will get desired results.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the modified version of above code:



and the output is



So the crux is : Whenever you are using an object with collections, always take care of hashcode and equals methods
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harpreet Singh janda wrote:So the crux is : Whenever you are using an object with collections, always take care of hashcode and equals methods

It was a great help Harpreet
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering how overriding equals() and hashcode() makes such a huge difference ?? Whats so special about equals and hashcode, that we should override them in user defined classes ??

Also which are those classes that override equals and hashcode , besides String and Wrapper Classes ?
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For two objects of a given class to be equal, they must have the same hashcode and meaningful equivalence defined within equal method. The default implementation in Object only gives two objects to be equal if they are of the same type and share the common reference.

For the exam purposes, it is useful to know that StringBuffer doesn't define equal method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic