Class PermutationIterator

java.lang.Object
com.landawn.abacus.util.PermutationIterator

public final class PermutationIterator extends Object
Copied from Google Guava. Provides static methods for working with Collection instances.
Since:
2.0
Author:
Chris Povirk, Mike Bostock, Jared Levy
  • Method Details

    • of

      public static <T> ObjIterator<List<T>> of(Collection<T> elements)
      Returns a Collection of all the permutations of the specified Collection.

      Notes: This is an implementation of the Plain Changes algorithm for permutations generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2.

      If the input list contains equal elements, some of the generated permutations will be equal.

      An empty collection has only one permutation, which is an empty list.

      Type Parameters:
      T -
      Parameters:
      elements - the original collection whose elements have to be permuted.
      Returns:
      an immutable Collection containing all the different permutations of the original collection.
      Throws:
      NullPointerException - if the specified collection is null or has any null elements.
      Since:
      12.0
    • ordered

      public static <T extends Comparable<? super T>> ObjIterator<List<T>> ordered(Collection<T> elements)
      Returns a Collection of all the permutations of the specified Iterable.

      Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.

      Duplicate elements are considered equal. For example, the list [1, 1] will have only one permutation, instead of two. This is why the elements have to implement Comparable.

      An empty iterable has only one permutation, which is an empty list.

      This method is equivalent to Collections2.orderedPermutations(list, Ordering.natural()).

      Type Parameters:
      T -
      Parameters:
      elements - the original iterable whose elements have to be permuted.
      Returns:
      an immutable Collection containing all the different permutations of the original iterable.
      Throws:
      NullPointerException - if the specified iterable is null or has any null elements.
      Since:
      12.0
    • ordered

      public static <T> ObjIterator<List<T>> ordered(Collection<T> elements, Comparator<? super T> comparator)
      Returns a Collection of all the permutations of the specified Iterable using the specified Comparator for establishing the lexicographical ordering.

      Examples:

         
      
         for (List<String> perm : orderedPermutations(asList("b", "c", "a"))) {
           println(perm);
         }
         // -> ["a", "b", "c"]
         // -> ["a", "c", "b"]
         // -> ["b", "a", "c"]
         // -> ["b", "c", "a"]
         // -> ["c", "a", "b"]
         // -> ["c", "b", "a"]
      
         for (List<Integer> perm : orderedPermutations(asList(1, 2, 2, 1))) {
           println(perm);
         }
         // -> [1, 1, 2, 2]
         // -> [1, 2, 1, 2]
         // -> [1, 2, 2, 1]
         // -> [2, 1, 1, 2]
         // -> [2, 1, 2, 1]
         // -> [2, 2, 1, 1]

      Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.

      Elements that compare equal are considered equal and no new permutations are created by swapping them.

      An empty iterable has only one permutation, which is an empty list.

      Type Parameters:
      T -
      Parameters:
      elements - the original iterable whose elements have to be permuted.
      comparator - a comparator for the iterable's elements.
      Returns:
      an immutable Collection containing all the different permutations of the original iterable.
      Throws:
      NullPointerException - If the specified iterable is null, has any null elements, or if the specified comparator is null.
      Since:
      12.0