Class PermutationIterator<E>
- java.lang.Object
-
- org.apache.commons.collections4.iterators.PermutationIterator<E>
-
- Type Parameters:
E
- the type of the objects being permuted
- All Implemented Interfaces:
java.util.Iterator<java.util.List<E>>
public class PermutationIterator<E> extends java.lang.Object implements java.util.Iterator<java.util.List<E>>
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 anUnsupportedOperationException
.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
Fields Modifier and Type Field Description private boolean[]
direction
Direction table used in the algorithm: false is left true is rightprivate int[]
keys
Permutation is done on theses keys to handle equal objects.private java.util.List<E>
nextPermutation
Next permutation to return.private java.util.Map<java.lang.Integer,E>
objectMap
Mapping between keys and objects.
-
Constructor Summary
Constructors Constructor Description PermutationIterator(java.util.Collection<? extends E> coll)
Standard constructor for this class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
hasNext()
Indicates if there are more permutation available.java.util.List<E>
next()
Returns the next permutation of the input collection.void
remove()
-
-
-
Field Detail
-
keys
private final int[] keys
Permutation is done on theses keys to handle equal objects.
-
objectMap
private final java.util.Map<java.lang.Integer,E> objectMap
Mapping between keys and objects.
-
direction
private final boolean[] direction
Direction table used in the algorithm:- false is left
- true is right
-
nextPermutation
private java.util.List<E> nextPermutation
Next permutation to return. When a permutation is requested this instance is provided and the next one is computed.
-
-
Constructor Detail
-
PermutationIterator
public PermutationIterator(java.util.Collection<? extends E> coll)
Standard constructor for this class.- Parameters:
coll
- the collection to generate permutations for- Throws:
java.lang.NullPointerException
- if coll is null
-
-
Method Detail
-
hasNext
public boolean hasNext()
Indicates if there are more permutation available.- Specified by:
hasNext
in interfacejava.util.Iterator<E>
- Returns:
- true if there are more permutations, otherwise false
-
next
public java.util.List<E> next()
Returns the next permutation of the input collection.- Specified by:
next
in interfacejava.util.Iterator<E>
- Returns:
- a list of the permutator's elements representing a permutation
- Throws:
java.util.NoSuchElementException
- if there are no more permutations
-
remove
public void remove()
- Specified by:
remove
in interfacejava.util.Iterator<E>
-
-