Source Code : Permutator test
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Permutator test
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
/**
*
* @author Andreou Dimitris, email: jim.andreou (at) gmail (dot) com
*/
public class Permutator {
private Permutator() { }
public static <T> Iterable<List<T>> permutations(final List<T> list) {
return new Iterable<List<T>>() {
public Iterator<List<T>> iterator() {
return new Iterator<List<T>>() {
private int current = 0;
private final long length = factorial(list.size());
public List<T> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
List<T> permutation = new ArrayList<T>(list);
int k = current;
for (int j = 2; j <= list.size(); j++) {
k /= j - 1;
Collections.swap(permutation, (k % j), j - 1);
}
current++;
return permutation;
}
public void remove() {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
return current < length;
}
};
}
};
}
private static long factorial(int k) {
long factorial = 1L;
for (int i = 2; i <= k; i++) {
factorial *= i;
}
return factorial;
}
}
// * @author Andreou Dimitris, email: jim.andreou (at) gmail (dot) com
/*
class PermutatorTest extends TestCase {
public PermutatorTest(String testName) {
super(testName);
}
public void testPermutations() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
Set<List<Integer>> allLists = new HashSet<List<Integer>>();
for (List<Integer> permutation : Permutator.permutations(list)) {
allLists.add(permutation);
}
assertEquals(allLists.size(), 1 * 2 * 3 * 4 * 5 * 6);
}
}
*/
Thank with us