How to create a cartesian product in Java?

I have an ArrayList number with each ArrayList having objects, and each one can have a different length. I need to create a permutation, as in the example below:

suppose i have 2 arraylist

arraylist A has object a, object b and object c
arraylist B has object d, object e

Then the output should consist of 6 new arraylist with these combinations:

combination 1 object a and object d,
combination 2 of object a and object e,
combination 3 of object b and object d,
combination 4 of object b and object e,
a combination of 5 objects c and object d,
combination 6 of object c and object e,

Can anybody help me?

+7
source share
4 answers

Guava 19+

 Lists.cartesianProduct(List...) 
.

eg

 List<Object> list1 = Arrays.asList("a", "b", "c"); List<Object> list2 = Arrays.asList("d", "e"); System.out.println(Lists.cartesianProduct(list1, list2)); 

Exit

 [[a, d], [a, e], [b, d], [b, e], [c, d], [c, e]] 
+6
source

With Java8 threads

  List<String> a = Arrays.asList("a", "b", "c"); List<String> b = Arrays.asList("d", "e"); String[][] AB = a.stream().flatMap(ai -> b.stream().map(bi -> new String[] { ai, bi })).toArray(String[][]::new); System.out.println(Arrays.deepToString(AB)); 

Exit

  [[a, d], [a, e], [b, d], [b, e], [c, d], [c, e]] 

To get as List

  List<List<String>> ll = a.stream().flatMap(ai -> b.stream().map(bi -> new ArrayList<>(Arrays.asList(ai, bi)))).collect(Collectors.toList()); 
+5
source

With Iterable + Iterator:

 import java.util.*; class CartesianIterator <T> implements Iterator <List <T>> { private final List <List <T>> lilio; private int current = 0; private final long last; public CartesianIterator (final List <List <T>> llo) { lilio = llo; long product = 1L; for (List <T> lio: lilio) product *= lio.size (); last = product; } public boolean hasNext () { return current != last; } public List <T> next () { ++current; return get (current - 1, lilio); } public void remove () { ++current; } private List<T> get (final int n, final List <List <T>> lili) { switch (lili.size ()) { case 0: return new ArrayList <T> (); // no break past return; default: { List <T> inner = lili.get (0); List <T> lo = new ArrayList <T> (); lo.add (inner.get (n % inner.size ())); lo.addAll (get (n / inner.size (), lili.subList (1, lili.size ()))); return lo; } } } } class CartesianIterable <T> implements Iterable <List <T>> { private List <List <T>> lilio; public CartesianIterable (List <List <T>> llo) { lilio = llo; } public Iterator <List <T>> iterator () { return new CartesianIterator <T> (lilio); } } 

You can use them in a simplified for-loop:

 class CartesianIteratorTest { public static void main (String[] args) { List <Character> la = Arrays.asList (new Character [] {'a', 'b', 'c'}); List <Character> lb = Arrays.asList (new Character [] {'d', 'e'}); List <List <Character>> llc = new ArrayList <List <Character>> (); llc.add (la); llc.add (lb); CartesianIterable <Character> ci = new CartesianIterable <Character> (llc); for (List<Character> lo: ci) show (lo); } public static void show (List <Character> lo) { System.out.print ("("); for (Object o: lo) System.out.print (o); System.out.println (")"); } } 
+4
source

Use nested for loops that will have a loop for each ArrayList, as shown below. I assume that I have two ArrayLists - intList and stringList. To generate a permutation, I can have two nested loops (one for each list).

  for(Integer i : intList){ for (String s : stringList) { ... } } 
0
source

All Articles