Java Iterator nextPermutation(Iterator> it)

Here you can find the source of nextPermutation(Iterator> it)

Description

next Permutation

License

BSD License

Declaration

public static <T> ArrayList<ArrayList<T>> nextPermutation(Iterator<? extends Collection<T>> it) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static <T> ArrayList<ArrayList<T>> nextPermutation(Iterator<? extends Collection<T>> it) {
        ArrayList<ArrayList<T>> resultLists = new ArrayList<ArrayList<T>>();

        if (it.hasNext()) {
            Collection<T> next = it.next();
            ArrayList<ArrayList<T>> remainingLists = nextPermutation(it);

            for (T inst : next) {
                for (ArrayList<T> list : remainingLists) {
                    ArrayList<T> resultList = new ArrayList<T>();
                    resultLists.add(resultList);
                    resultList.add(inst);
                    resultList.addAll(list);
                }//from ww  w .  j a va2  s.com
            }

        } else {
            ArrayList<T> resultList = new ArrayList<T>();
            resultLists.add(resultList);
        }

        return resultLists;
    }
}

Related

  1. moveNext(Iterator iterator)
  2. multiIterator(Iterator... iterable)
  3. multiply(String string, Iterator placeholders, Iterator replacements)
  4. multiply(String string, Iterator placeholders, Iterator replacements)
  5. multiSetIterator(Iterator... iterable)
  6. printContainer(Iterator it, String sep)
  7. printElements(String desc, Iterator i)
  8. printIterator(final Iterator iterator)
  9. printIterator(Iterator i, String header)