T
- The type of element that the combinations are made from.public class CombinationGenerator<T> extends Object implements Iterable<List<T>>
PermutationGenerator
Constructor and Description |
---|
CombinationGenerator(Collection<T> elements,
int combinationLength)
Create a combination generator that generates all combinations of
a specified length from the given set.
|
CombinationGenerator(T[] elements,
int combinationLength)
Create a combination generator that generates all combinations of
a specified length from the given set.
|
Modifier and Type | Method and Description |
---|---|
long |
getRemainingCombinations() |
long |
getTotalCombinations() |
boolean |
hasMore()
Are there more combinations?
|
Iterator<List<T>> |
iterator()
Provides a read-only iterator for iterating over the combinations
generated by this object.
|
T[] |
nextCombinationAsArray()
Generate the next combination and return an array containing
the appropriate elements.
|
T[] |
nextCombinationAsArray(T[] destination)
Generate the next combination and return an array containing
the appropriate elements.
|
List<T> |
nextCombinationAsList()
Generate the next combination and return a list containing the
appropriate elements.
|
List<T> |
nextCombinationAsList(List<T> destination)
Generate the next combination and return a list containing
the appropriate elements.
|
void |
reset()
Reset the combination generator.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public CombinationGenerator(T[] elements, int combinationLength)
elements
- The set from which to generate combinations.combinationLength
- The length of the combinations to be generated.public CombinationGenerator(Collection<T> elements, int combinationLength)
elements
- The set from which to generate combinations.combinationLength
- The length of the combinations to be generated.public final void reset()
public long getRemainingCombinations()
public boolean hasMore()
public long getTotalCombinations()
public T[] nextCombinationAsArray()
nextCombinationAsArray(Object[])
,
nextCombinationAsList()
public T[] nextCombinationAsArray(T[] destination)
nextCombinationAsArray()
method is
used it will create a new array every time. When iterating over
combinations this will result in lots of short-lived objects that
have to be garbage collected. This method allows a single array
instance to be reused in such circumstances.destination
- Provides an array to use to create the
combination. The specified array must be the same length as a
combination.public List<T> nextCombinationAsList()
nextCombinationAsList(List)
,
nextCombinationAsArray()
public List<T> nextCombinationAsList(List<T> destination)
nextCombinationAsList()
method is
used it will create a new list every time. When iterating over
combinations this will result in lots of short-lived objects that
have to be garbage collected. This method allows a single list
instance to be reused in such circumstances.destination
- Provides a list to use to create the
combination.public Iterator<List<T>> iterator()
Provides a read-only iterator for iterating over the combinations
generated by this object. This method is the implementation of the
Iterable
interface that permits instances of this class to be
used with the new-style for loop.
For example:
List<Integer> elements = Arrays.asList(1, 2, 3); CombinationGenerator<Integer> combinations = new CombinationGenerator(elements, 2); for (List<Integer> c : combinations) { // Do something with each combination. }
Copyright © 2015. All rights reserved.