• 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

Standard idiom for getting subsets of a set ?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a SortedSet<String> and would like to check a property for all of its subsets containing one string less than the set itself. Is there a standard way to do this ?

I don't like my idea below, which I haven't tried compiling yet. Since I am removing from the set while iterating, I have to use an Iterator rather than the simpler for-each loop.


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that's going to work. Removing the item via the iterator is fine - but then you're adding it back again within the loop, and that's also going to be a problem.

One approach that should work would be to take a copy of the set values first - e.g. by using toArray(). Then you can iterate through the copy, and within that loop you're free to add and remove items from the original set.

 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can just create a new TreeSet to be the subset. Iterate through the original set normally; if the originalset contains an element for which hasInterestingProperty() is true, add it to the subset.

If you use Google Collections, you can do this:
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that's quite the requirement - hasInterestingProperty refers to each possible subset (of size n - 1), not to individual elements.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I read the question too quickly. Never mind.
 
Steiner Egdal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right about this not being legal. The code below



compiles, but results in



My problem is that I will be working with many set and they will be large, so I was hoping there was some trick to avoid creating the whole subsets,
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create an array copy of the Set as Matthew suggested and then do your processing on that. Rather than removing an element from the array you could just ignore a specific element each time.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic