2

I have two lists:

List<Integer> partnerIdList;
List<Integer> platformIdList;

I need to get a Cartesian product of those list as follows:

List<Pair<Integer, Integer> > partnerPlatformPairList;

Where Pair is a class from the org.apache.commons.lang3.tuple.Pair package.

How can I easily do that? Is there some in the apache-commons library?

Community
  • 1
  • 1
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 1
    Something along the lines of cartesianProduct in http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Sets.html may be useful to you – Laurentiu L. May 18 '15 at 09:46
  • Kindly Refer for a similar solution: [Cartesian product of List of Lists in Java][1] [1]: http://stackoverflow.com/questions/9591561/java-cartesian-product-of-a-list-of-lists – Balkrishan Aggarwal May 18 '15 at 09:46

4 Answers4

2

There is a github code. You can look into it. It basically runs, for-loop based on number of list and list count. It will reduce your coding effort, but basics remain the same.

or

Use the following code

for (int i = 0; i < partnerIdList.size(); i++)
    for (int j = 0; j < platformIdList.size(); j++)
        partnerPlatformPairList.add(new Pair<Integer, Integer>(partnerIdList.get(i), platformIdList.get(j)));
Community
  • 1
  • 1
Abhishek
  • 6,912
  • 14
  • 59
  • 85
1

If you don't want to use the external solutions, libraries, you may write your own version in code:

public static <T, U> List<Pair<T, U>> cartesianProduct(List<T> list1, List<U> list2) {
    List<Pair<T, U>> result = new ArrayList<>();
    for (T el1: list1) {
        for (U el2 : list2) {
            result.add(Pair.of(el1, el2));
        }
    }
    return result;
}
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
1
public void cartesian() {

    List<Integer> array1 = new ArrayList<Integer>();

    List<Integer> array2 = new ArrayList<Integer>();

    List<Pair<Integer, Integer>> partnerPlatformPairList = new ArrayList<Pair<Integer, Integer>>();

    for (int i = 0; i < array1.size(); i++)
        for (int j = 0; j < array2.size(); j++)
            partnerPlatformPairList.add(new Pair<Integer, Integer>(array1.get(i), array2
                    .get(j)));

}
Rajesh
  • 2,135
  • 1
  • 12
  • 14
1
import java.util.ArrayList;
import java.util.List;

public class Track {
  public static void main(String x[]) {
    List<Integer> partnerIdList = new ArrayList<Integer>();
    List<Integer> platformIdList = new ArrayList<Integer>();

    for (int i = 2; i < 5; i++) {
      partnerIdList.add(i);
      platformIdList.add(i * i);
    }
    List<Pair<Integer, Integer>> partnerPlatformPairList = new ArrayList<Pair<Integer, Integer>>();

    for (Integer partnerId : partnerIdList) {
      for (Integer platformId : platformIdList) {
        partnerPlatformPairList.add(new Pair(partnerId, platformId));
      }
    }

    for (Pair pair : partnerPlatformPairList) {
      System.out.println(pair);
    }
  }
}

class Pair<Integer1, Integer2> {
  Integer partnerId;
  Integer platformId;

  Pair(Integer partnerId, Integer platformId) {
    this.partnerId = partnerId;
    this.platformId = platformId;
  }

  @Override
  public String toString() {
    return partnerId + " -  " + platformId;
  }
}
Community
  • 1
  • 1