Class PermutationIterator<E>

  • Type Parameters:
    E - the type of the objects being permuted
    All Implemented Interfaces:

    public class PermutationIterator<E>
    extends Object
    implements Iterator<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 an UnsupportedOperationException.

    NOTE: in case an empty collection is provided, the iterator will return exactly one empty list as result, as 0! = 1.

    • Constructor Detail

      • PermutationIterator

        public PermutationIterator​(Collection<? extends E> coll)
        Standard constructor for this class.
        coll - the collection to generate permutations for
        NullPointerException - if coll is null
    • Method Detail

      • hasNext

        public boolean hasNext()
        Indicates if there are more permutation available.
        Specified by:
        hasNext in interface Iterator<E>
        true if there are more permutations, otherwise false
      • next

        public List<E> next()
        Returns the next permutation of the input collection.
        Specified by:
        next in interface Iterator<E>
        a list of the permutator's elements representing a permutation
        NoSuchElementException - if there are no more permutations
      • remove

        public void remove()
        Specified by:
        remove in interface Iterator<E>