Class Permutations<E>

  • All Implemented Interfaces:
    java.util.Iterator<java.util.List<E>>

    public class Permutations<E>
    extends java.lang.Object
    implements java.util.Iterator<java.util.List<E>>
    http://code.google.com/p/google-collections/ Apache License 2.0 The Permutations class provides an iteration of all permutations of an list of objects. Each permutation is simply an ordered list of the group.

    For example, to see all of the ways we can select a school representative and an alternate from a list of 4 children, begin with an array of names::

         List children = Collections.asList({Leonardo, Monica, Nathan, Olivia});
     
    To see all 2-permutations of these 4 names, create and use a Permutations enumeration:
     Permutations c = new Permutations(children, 2);
     while (c.hasNext()) {
      List perm = c.next();
      for (int i = 0; i < perm.size(); i++) {
        System.out.print(perm.get(i) +  );
      }
     System.out.println();
     }
     
    This will print out:
     Leonardo Monica
     Leonardo Nathan
     Leonardo Olivia
     Monica Leonardo
     Monica Nathan
     Monica Olivia
     Nathan Leonardo
     Nathan Monica
     Nathan Olivia
     Olivia Leonardo
     Olivia Monica
     Olivia Nathan
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Permutations​(java.util.List<E> inList)
      Create a Permutation to iterate through all possible lineups of the supplied array of Objects.
      Permutations​(java.util.List<E> inList, int m)
      Create a Permutation to iterate through all possible lineups of the supplied array of Objects.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()  
      java.util.List<E> next()  
      void remove()  
      • 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
    • Constructor Detail

      • Permutations

        public Permutations​(java.util.List<E> inList)
        Create a Permutation to iterate through all possible lineups of the supplied array of Objects.
        Parameters:
        Object - [] inArray the group to line up
        Throws:
        CombinatoricException - Should never happen with this interface
      • Permutations

        public Permutations​(java.util.List<E> inList,
                            int m)
        Create a Permutation to iterate through all possible lineups of the supplied array of Objects.
        Parameters:
        Object - [] inArray the group to line up
        inArray - java.lang.Object[], the group to line up
        m - int, the number of objects to use
        Throws:
        CombinatoricException - if m is greater than the length of inArray, or less than 0.
    • Method Detail

      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<E>
        Returns:
        true, unless we have already returned the last permutation.
      • next

        public java.util.List<E> next()
        Specified by:
        next in interface java.util.Iterator<E>
        Returns:
        java.lang.Object, the next permutation of the original Object array.

        Actually, an array of Objects is returned. The declaration must say just Object, because the Permutations class implements Iterator, which declares that the next() returns a plain Object. Users must cast the returned object to (Object[]).

      • remove

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