Generating Cartesian Product Set in Java

Few days back, I received an interesting problem from my business team. They wanted create combo items from several items having several SKUs. They wanted sets of Cartesian sets. Came up with this possible solution with Google-Guava 14.0rc1 with JDK 6.0 on Netbeans 7.2.1.

public static void main(String[] args) {

     List<String> s1 = Arrays.asList(“001”, “002”, “003”);

     List<String> s2 = Arrays.asList(“001”, “002”);

     List<String> s3 = Arrays.asList(“001”, “002”, “003”);

     List<String> s4 = Arrays.asList(“001”, “002”);

     List<Set<String>> sets = new ArrayList<Set<String>>();

     sets.add(new HashSet(s1));

     sets.add(new HashSet(s2));

     sets.add(new HashSet(s3));

     sets.add(new HashSet(s4));

     Set<List<String>> cartesianSet =;

     Map<String, List<String>> comboMap = new HashMap<String, List<String>>();

     int count = 0;

     for(List<String> element : cartesianSet ){

         count = count + 1;

         System.out.print(String.format(“%03d”, count));

         System.out.print(” “);


         comboMap.put(String.format(“%03d”, count), element);




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s