【问题标题】:Use of guava Sets.cartesianProduct with unknown number of arguments使用具有未知数量参数的番石榴 Sets.cartesianProduct
【发布时间】:2018-03-17 09:13:03
【问题描述】:

我遇到了以下问题:

我有一个动态的项目列表,定义如下:

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

Object1Object2 在这里不感兴趣,但数组 Double[] 包含任意数量的条目。

在我的代码中,我遍历外部ArrayList 并尝试计算番石榴的笛卡尔积。到目前为止,我有类似的东西(部分代码不起作用,对不起......):

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;
}

在我找到的所有示例中,他们总是用已知的集合调用笛卡尔产品,而我不能这样做:

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

我最后想要的是存储在内部 Double[] 数组中的数字的所有组合。

任何帮助表示赞赏。

【问题讨论】:

标签: java guava cartesian-product


【解决方案1】:

感谢the Post "Java Guava CartesianProduct" 我解决了我的问题。我的最终解决方案如下所示:

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);
}

另外我更改了输入列表的格式:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多