• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

doubts on how set and map avoid duplicates

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

I am not getting how does Set and Map prevent duplicate entry additions for custom made classes.

Is it correct to say that for a set and Map keys to work correctly .
BOTH equals and hashCode method shall be implemented correctly according to hashCode implementation contract.

Further if any of the following will result in an wrong behivour

1. Only equals method is implemented
2. only hashCode method is implemented
3. equals and hashCode method does not obey correct implemetation contract.
4. Both equals and hashCode methods are NOT implemented.

I am facing problem in solving questions which add multiple instances of same class and then ask what will size of a set or Map. SO require help how to master this concept.

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

It sounds like the piece you might be missing is this: if you don't define one or both of equals() or hashcode(), the method(s) you don't define are inherited from Object. The definitions of those methods in the Object class are not necessarily what you need for your application (just what does "equal" really mean in your app? what should constitute a "duplicate"?). But, the Object class implementations are consistent with each other, and fulfill the "hashcode contract".

So, to address your 4 cases:

1. Only equals method is implemented
2. only hashCode method is implemented
3. equals and hashCode method does not obey correct implemetation contract.
4. Both equals and hashCode methods are NOT implemented.

Case 1 & 2:
it depends on whether the method that is implemented is done in a way that is still "consistent with" the other method definition, inherited from Object. If the two methods, taken together, still abide by the contract, your Map and Set will work as expected.

More likely, this is not the case - usually one of these methods is overridden in a way that is meaningful for that particular class (considering certain key instance variables), and end up no longer being consistent with the inherited implementation of the other method.

Case 3:
The HashMap and HashSet will not work as expected in this case.

Case 4:
will work "properly" with a Map and Set, assuming you want things to be considered "duplicates" or not based on the Object.equals() definition... duplicates would be prevented when that method would return true (that is, they are the exact same object instance).


Hope this helps,
-- Jon
 
pie. 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