• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LinkedHashSet

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was thinking LinkedHashSet doesn't allow duplicates.


How do you know if a Lion is equal to another ?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic