T
- The type of element that the permutation will consist of.public class PermutationGenerator<T> extends Object implements Iterable<List<T>>
BigInteger
).CombinationGenerator
Constructor and Description |
---|
PermutationGenerator(Collection<T> elements)
Permutation generator that generates all possible orderings of
the elements in the specified set.
|
PermutationGenerator(T[] elements)
Permutation generator that generates all possible orderings of
the elements in the specified set.
|
Modifier and Type | Method and Description |
---|---|
long |
getRemainingPermutations()
Returns the number of permutations not yet generated.
|
long |
getTotalPermutations()
Returns the total number of unique permutations that can be
generated for the given set of elements.
|
boolean |
hasMore()
Are there more permutations that have not yet been returned?
|
Iterator<List<T>> |
iterator()
Provides a read-only iterator for iterating over the permutations
generated by this object.
|
T[] |
nextPermutationAsArray()
Generate the next permutation and return an array containing
the elements in the appropriate order.
|
T[] |
nextPermutationAsArray(T[] destination)
Generate the next permutation and return an array containing
the elements in the appropriate order.
|
List<T> |
nextPermutationAsList()
Generate the next permutation and return a list containing
the elements in the appropriate order.
|
List<T> |
nextPermutationAsList(List<T> destination)
Generate the next permutation and return a list containing
the elements in the appropriate order.
|
void |
reset()
Resets the generator state.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public PermutationGenerator(T[] elements)
elements
- The elements to permute.public PermutationGenerator(Collection<T> elements)
elements
- The elements to permute.public final void reset()
public long getRemainingPermutations()
public long getTotalPermutations()
public boolean hasMore()
public T[] nextPermutationAsArray()
nextPermutationAsArray(Object[])
,
nextPermutationAsList()
public T[] nextPermutationAsArray(T[] destination)
nextPermutationAsArray()
method is
used it will create a new array every time. When iterating over
permutations 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
permutation. The specified array must be the same length as a
permutation. This is the array that will be returned, once
it has been filled with the elements in the appropriate order.public List<T> nextPermutationAsList()
nextPermutationAsList(java.util.List)
,
nextPermutationAsArray()
public List<T> nextPermutationAsList(List<T> destination)
nextPermutationAsList()
method is
used it will create a new list every time. When iterating over
permutations 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
permutation. This is the list that will be returned, once
it has been filled with the elements in the appropriate order.public Iterator<List<T>> iterator()
Provides a read-only iterator for iterating over the permutations
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); PermutationGenerator<Integer> permutations = new PermutationGenerator(elements); for (List<Integer> p : permutations) { // Do something with each permutation. }
Copyright © 2015. All rights reserved.