Here you can find the source of nextPermutation(Iterator extends Collection
public static <T> ArrayList<ArrayList<T>> nextPermutation(Iterator<? extends Collection<T>> it)
//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; } }