Class PermutationIterator<E>
java.lang.Object
org.apache.commons.collections4.iterators.PermutationIterator<E>
- Type Parameters:
E
- the type of the objects being permuted
This iterator creates permutations of an input collection, using the
Steinhaus-Johnson-Trotter algorithm (also called plain changes).
The iterator will return exactly n! permutations of the input collection.
The remove()
operation is not supported, and will throw an
UnsupportedOperationException
.
NOTE: in case an empty collection is provided, the iterator will return exactly one empty list as result, as 0! = 1.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean[]
Direction table used in the algorithm: false is left true is rightprivate final int[]
Permutation is done on theses keys to handle equal objects.Next permutation to return.Mapping between keys and objects. -
Constructor Summary
ConstructorsConstructorDescriptionPermutationIterator
(Collection<? extends E> coll) Standard constructor for this class. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Field Details
-
keys
private final int[] keysPermutation is done on theses keys to handle equal objects. -
objectMap
Mapping between keys and objects. -
direction
private final boolean[] directionDirection table used in the algorithm:- false is left
- true is right
-
nextPermutation
Next permutation to return. When a permutation is requested this instance is provided and the next one is computed.
-
-
Constructor Details
-
PermutationIterator
Standard constructor for this class.- Parameters:
coll
- the collection to generate permutations for- Throws:
NullPointerException
- if coll is null
-
-
Method Details
-
hasNext
public boolean hasNext()Indicates if there are more permutation available. -
next
Returns the next permutation of the input collection.- Specified by:
next
in interfaceIterator<E>
- Returns:
- a list of the permutator's elements representing a permutation
- Throws:
NoSuchElementException
- if there are no more permutations
-
remove
public void remove()
-