Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding cartesian product in Java

I want to find cartesian product of set of elements. Here's an example

example 1:

sets : (ab) (bc) (ca)

cartesian product is:

abc aba acc aca bbc bba bcc bca

example 2:

sets : (zyx) b c

cartesian product is:

zbc ybc xbc

So I am thinking of an algorithm to execute in Java which can find cartesian product of particular amount of groups defined at compile time at the start.

like image 314
rockvilla Avatar asked Jul 03 '11 14:07

rockvilla


1 Answers

You can use the Sets.cartesianProduct() method from Google's Guava libraries to generate Cartesian products:

com.google.common.collect.Sets.cartesianProduct(Set[] yourSets)

If only everything was that easy!

like image 85
Neil Avatar answered Sep 28 '22 02:09

Neil