• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Collections question

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to take two sets A & B and remove from the set B
all elements common to A & B. Is there a clean way to do this using java.util.Collections?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alok Pota:
I would like to take two sets A & B and remove from the set B
all elements common to A & B. Is there a clean way to do this using java.util.Collections?


Assuming that A abd B both implement the Set interface (HashSet, TreeSet), you can just do:
B.removeAll(A);
If it ends up removing any objects, the method will return true. Also, the List interface has a removeAll method, so I think you're covered for any standard Java class implementing Collection. I don't think you need the Collections class.

 
reply
    Bookmark Topic Watch Topic
  • New Topic