2

I'm stuck in the following problem:

I have a dynamic List of items defined like this:

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, Double[]>)

The Object1 and Object2 are not of interest here, but the array Double[] which contains an arbitrary number of entries.

In my code I iterate over the outer ArrayList and try to calculate the cartesianProduct of guava. Up to now I have something like (partly not working code, sorry ...):

private Set<List<Set<Double>>> getValueCombinations() {
    List<Set<Double>> valuesOfInnerArrays = new ArrayList<>();
    // Loop over the list of device data sets in the class and add the trim value vectors to a list for further
    // processing and cartesian prooduct generation.
    for (Integer indexCounter = 0; indexCounter < OuterArrayList.size(); indexCounter++) {
        final List<Object> innerDataSet = (List<Object>) OuterArrayList.get(indexCounter);
        final Set<Double> innerDoubleArray = ImmutableSet.of(((List<Double>) innerDataSet.get(2)).toArray(new Double[]));
        valuesOfInnerArrays.add(innerDoubleArray);
    }
    ImmutableList<Set<Double>> test = ImmutableList.of(valuesOfInnerArrays)
    // generate Cartesian product of all trim vectors = a n x m matrix of all combinations of settings
    final Set<List<Set<Double>>> cartesianProduct = Sets.cartesianProduct(ImmutableList.of(valuesOfInnerArrays));

    return cartesianProduct;
}

In all the examples I found, they always call cartesianProduct with known Sets, which I cannot do:

Set<Double> first = ImmutableSet.of(1., 2.);
Set<Double> second = ImmutableSet.of(3., 4.);
Set<List<Double>> result =
Sets.cartesianProduct(ImmutableList.of(first, second));

What I would like to have in the end are all cominations of the numbers stored in the inner Double[] arrays.

Any help appreciated.

WolfiG
  • 1,059
  • 14
  • 31
  • Does this answer your question? [Iteratively compute the Cartesian product of an arbitrary number of sets](https://stackoverflow.com/questions/1719594/iteratively-compute-the-cartesian-product-of-an-arbitrary-number-of-sets) –  Apr 22 '21 at 18:33

1 Answers1

2

Thanks to the Post "Java Guava CartesianProduct" I solved my problem. My final solution looks like this:

private Set<List<Double>> getValueCombinations() {
    final List<Set<Double>> valuesOfInnerArrays = new ArrayList<>();
    // Loop over the list of device data sets in the class and add the value vectors to a list for further
    // processing and cartesian product generation.
    for (Integer indexCounter = 0; indexCounter < outerArrayList.size(); indexCounter++) {
        final List<Object> innerDataSet = (List<Object>) deviceDataSets.get(indexCounter);
        final SortedSet<Double> >innerDoubleArray = new TreeSet<>((List<Double>) innerDataSet.get(2));
        valuesOfInnerArrays.add(innerDoubleArray);
    }

    return Sets.cartesianProduct(valuesOfInnerArrays);
}

Additionally I changed the format of my input list:

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, List<Double>>)
WolfiG
  • 1,059
  • 14
  • 31