• 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

replace-method in java.util.set

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does java.util.Set has not a replace-method?

I want to replace a object being already in the set.

so I have to remove and add it again, this disturbs my ordering.

so I have to use linkedSet?!

would be easier with a method replace.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What has ordering got to do with a Set? Sets do not support order, so a replace method would be pointless. In fact it would be difficult to implement for something like HashSet. Using remove() and add() is the correct way to do it.
LinkedHashSet is a hybrid, a set combined with a sequence. But even that, which maintains original insertion order (ignoring re-insertion) does not support replace.
If you need a replace(E, E) method, override Set<E> to add a replace method and use an instance of that interface as a wrapper round a HashSet object.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats interesting, thanks!

So I do not have to instantiate the Set within MySet?



When using a Instance of MySet, which one should I use:



or that


 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither.

You would be looking to write your own interface which extends Set<E> with a boolean replace(E oldElement, E newElement) method.And yes, of course you will have to instantiate the Set instance which your class wraps. I presumed you would know that was included in the ellipses ". . .".
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic