E
- the type of the objects being permutedpublic class PermutationIterator<E>
extends java.lang.Object
implements java.util.Iterator<java.util.List<E>>
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.
Modifier and Type | Field and Description |
---|---|
private boolean[] |
direction
Direction table used in the algorithm:
false is left
true is right
|
private 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 and Description |
---|
PermutationIterator(java.util.Collection<? extends E> coll)
Standard constructor for this class.
|
Modifier and Type | Method and 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() |
private int[] keys
private java.util.Map<java.lang.Integer,E> objectMap
private boolean[] direction
private java.util.List<E> nextPermutation
public PermutationIterator(java.util.Collection<? extends E> coll)
coll
- the collection to generate permutations forjava.lang.NullPointerException
- if coll is nullpublic boolean hasNext()
hasNext
in interface java.util.Iterator<java.util.List<E>>
public java.util.List<E> next()
next
in interface java.util.Iterator<java.util.List<E>>
java.util.NoSuchElementException
- if there are no more permutationspublic void remove()
remove
in interface java.util.Iterator<java.util.List<E>>