T
- The type of element that the combinations are made from.public class CombinationGenerator<T>
extends java.lang.Object
implements java.lang.Iterable<java.util.List<T>>, java.io.Serializable
PermutationGenerator
,
Serialized FormConstructor and Description |
---|
CombinationGenerator(java.util.Collection<T> elements,
int combinationLength,
T[] filler)
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?
|
java.util.Iterator<java.util.List<T>> |
iterator()
Provides a read-only iterator for iterating over the combinations
generated by this object.
|
T[] |
nextCombinationAsArray(T[] destination)
Generate the next combination and return an array containing
the appropriate elements.
|
java.util.List<T> |
nextCombinationAsList()
Generate the next combination and return a list containing the
appropriate elements.
|
java.util.List<T> |
nextCombinationAsList(java.util.List<T> destination)
Generate the next combination and return a list containing
the appropriate elements.
|
void |
reset()
Reset the combination generator.
|
public CombinationGenerator(T[] elements, int combinationLength)
elements
- The set from which to generate combinations; will be used directly (not copied)combinationLength
- The length of the combinations to be generated.public CombinationGenerator(java.util.Collection<T> elements, int combinationLength, T[] filler)
elements
- The set from which to generate combinations.combinationLength
- The length of the combinations to be generated.filler
- An array of T with the same length as elements or less (often 0);
needed because GWT can't create a generic array. If elements is not
a Collection defined in the JDK (by GWT), then this should have
exactly the same length as elements, since GWT can create larger
versions of an array on its own, but our code can't easily.public final void reset()
public long getRemainingCombinations()
public boolean hasMore()
public long getTotalCombinations()
public T[] nextCombinationAsArray(T[] destination)
destination
- Provides an array to use to create the
combination. The specified array must be the same length as a
combination.public java.util.List<T> nextCombinationAsList()
nextCombinationAsList(List)
public java.util.List<T> nextCombinationAsList(java.util.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 java.util.Iterator<java.util.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. }
iterator
in interface java.lang.Iterable<java.util.List<T>>
Copyright © Eben Howard 2012–2022. All rights reserved.