Java tutorial
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static <T> Collection<List<T>> combinations(List<Collection<T>> domains) { List<List<T>> perms = new ArrayList<List<T>>(); perms.add(new ArrayList<T>()); addCombination(perms, domains, 0); return perms; } private static <T> void addCombination(List<List<T>> perms, List<Collection<T>> domains, int i) { if (i < domains.size()) { for (int permIndex = 0; permIndex < perms.size();) { List<T> perm = perms.get(permIndex); perms.remove(permIndex); if (domains.get(i).isEmpty()) { perms.clear(); return; } for (T domValue : domains.get(i)) { List<T> newPerm = new ArrayList<T>(perm); newPerm.add(domValue); perms.add(permIndex++, newPerm); } } addCombination(perms, domains, i + 1); } } }