fork download
  1. import java.util.List;
  2. import java.util.LinkedList;
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6. public static void main (String[] args) {
  7. List<List<String>> input = Arrays.asList(
  8. Arrays.asList("England"),
  9. Arrays.asList("London", "Liverpool"),
  10. Arrays.asList("DG300", "SS500")
  11. );
  12.  
  13. for (List<String> output : combinations(input)) {
  14. System.out.println(output);
  15. }
  16. }
  17.  
  18. // combinations :: [[String]] -> [[String]]
  19. public static List<List<String>> combinations(List<List<String>> values) {
  20.  
  21. // combinations [] = [[]]
  22. if (values.isEmpty()) {
  23. return Arrays.asList(Arrays.asList());
  24. }
  25.  
  26. // combinations (x:xs) =
  27. List<String> x = values.get(0);
  28. List<List<String>> xs = values.subList(1, values.size());
  29.  
  30. // do
  31. List<List<String>> outputs = new LinkedList<>();
  32.  
  33. // ys <- combinations xs
  34. for (List<String> ys : combinations(xs)) {
  35.  
  36. // y <- x
  37. for (String y : x) {
  38.  
  39. // (y:ys)
  40. List<String> output = new LinkedList<>();
  41. output.add(y);
  42. output.addAll(ys);
  43.  
  44. // return
  45. outputs.add(output);
  46. }
  47. }
  48.  
  49. return outputs;
  50. }
  51. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
[England, London, DG300]
[England, Liverpool, DG300]
[England, London, SS500]
[England, Liverpool, SS500]