• 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

Regarding set interface

 
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 want to allow duplicate elements in set object.Is it possible to allow duplicates in set.If yes how can we do this?

Regards,
Rama Krishna.Y
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I'm afraid the whole point of the Set interface is a collection with no duplicates.

If you want duplicates, the easiest approach within the core Java libraries would be to use a List (and ignore the fact that there's an ordering to it). The general term for a collection with duplicates and no ordering is a "Bag", but that's not available in core Java, I don't think. You can probably get free libraries that contain one (e.g. a quick search shows that Apache Commons has one).
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you can do........
what Set do is checked duplicate values using equals method and hashcode method.......you can override the equals method in an appropirate way
to make it possible.........


can anybody comment if i am wrong
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If he went and changed the equals method to something that doesn't list duplicates as duplicates... they wouldn't be duplicates anymore, would they? Because you only recognize duplicates through their equals method.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:If he went and changed the equals method to something that doesn't list duplicates as duplicates... they wouldn't be duplicates anymore, would they? .




cant understand.....please elaborate..its confusing
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set uses the equals method to decide which objects are equal. Two objects will be considered duplicate if your equals method returns true when they are compared. If you override equals method in such a way that either it always returns false, then you can add the same object to the Set twice...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i hope it must be clear now??

cheers
Shanky
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic