T
- type of elements in set and generated subsetspublic class SubsetIterator<T> extends Object implements Iterator<Set<T>>
A subset iterator generates all possible subsets of a given set, within the imposed size range.
The implemented generation algorithm is the revolving door algorithm from "Combinatorial Algorithms: Generation, Enumeration and Search", Donald Kreher and Douglas Stinson, CRC Press, 1999 (chapter 2, p. 43-52). This algorithm generates k-subsets in a specific minimal change ordering called the revolving door ordering.
Constructor and Description |
---|
SubsetIterator(Set<T> items,
int fixedSubsetSize)
Create a subset iterator that generates all subsets of the given set, with a fixed size.
|
SubsetIterator(Set<T> items,
int minSubsetSize,
int maxSubsetSize)
Create a subset iterator that generates all subsets of the given set, within the imposed size range.
|
Modifier and Type | Method and Description |
---|---|
boolean |
hasNext()
Checks whether more subsets are to be generated.
|
Set<T> |
next()
Generate the next subset in a newly allocated
LinkedHashSet . |
Collection<T> |
next(Collection<T> subset)
Fill the given collection with the items from the next subset.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining, remove
public SubsetIterator(Set<T> items, int minSubsetSize, int maxSubsetSize)
items
- set of items to select fromminSubsetSize
- minimum subset sizemaxSubsetSize
- maximum subset sizeNullPointerException
- if items
is null
IllegalArgumentException
- if items
is empty,
minSubsetSize < 0
,
minSubsetSize > |IDs|
,
or minSubsetSize > maxSubsetSize
public SubsetIterator(Set<T> items, int fixedSubsetSize)
items
- set of items to select fromfixedSubsetSize
- fixed subset sizeNullPointerException
- if items
is null
IllegalArgumentException
- if items
is empty,
fixedSubsetSize < 0
,
or fixedSubsetSize > |items|
public boolean hasNext()
public Collection<T> next(Collection<T> subset)
Fill the given collection with the items from the next subset. The collection is not cleared so already contained items will be retained. A reference to this same collection is returned after it has been modified.
To store the next subset in a newly allocated LinkedHashSet
the alternative method
next()
may also be used.
subset
- collection to fill with items from next generated subsetNoSuchElementException
- if there is no next subset to be generatedpublic Set<T> next()
LinkedHashSet
.next
in interface Iterator<Set<T>>
LinkedHashSet
.NoSuchElementException
- if there is no next subset to be generatedCopyright © 2016. All rights reserved.