You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中从嵌套集合结构获取类笛卡尔积结果

解决Java嵌套集合的笛卡尔积问题

嘿,我来帮你搞定这个笛卡尔积的计算问题!先梳理下你的集合结构:你有一个curPage(类型是List<Map<String, List<String>>>),每个Map里存储着一个键对应一个字符串列表,最终你需要把所有这些列表的元素做笛卡尔积——也就是生成所有可能的元素组合,对吧?

下面分两步来实现:

第一步:扁平化嵌套集合,提取所有参与计算的列表

首先我们需要把curPage里嵌套的列表都提取出来,整理成一个List<List<String>>的结构,这样方便后续计算笛卡尔积:

// 从curPage中提取所有需要参与笛卡尔积的列表
List<List<String>> targetLists = new ArrayList<>();
for (Map<String, List<String>> singleMap : curPage) {
    // 遍历每个Map中的所有值(也就是各个列表)
    for (List<String> list : singleMap.values()) {
        targetLists.add(list);
    }
}

如果你的pageData(List<List<Integer>>)也需要参与笛卡尔积,只需要把它的元素转成List<String>(或者直接用后面的泛型版本方法)后加入targetLists即可。

第二步:实现笛卡尔积计算方法

Java标准库没有内置笛卡尔积的工具,我们可以用递归的方式实现一个简洁易懂的方法:

/**
 * 计算多个字符串列表的笛卡尔积
 * @param inputLists 输入的列表集合
 * @return 所有可能的元素组合列表
 */
public static List<List<String>> computeCartesianProduct(List<List<String>> inputLists) {
    List<List<String>> result = new ArrayList<>();
    
    // 递归终止条件:如果没有输入列表,返回包含空列表的结果作为基础
    if (inputLists.isEmpty()) {
        result.add(new ArrayList<>());
        return result;
    }
    
    // 取出第一个列表,递归计算剩余列表的笛卡尔积
    List<String> firstList = inputLists.get(0);
    List<List<String>> restProduct = computeCartesianProduct(inputLists.subList(1, inputLists.size()));
    
    // 将第一个列表的每个元素,和剩余列表的笛卡尔积结果逐一组合
    for (String item : firstList) {
        for (List<String> combination : restProduct) {
            List<String> newCombination = new ArrayList<>();
            newCombination.add(item);
            newCombination.addAll(combination);
            result.add(newCombination);
        }
    }
    
    return result;
}

调用示例

把提取好的targetLists传入方法,就能得到笛卡尔积结果了:

// 计算笛卡尔积
List<List<String>> cartesianResult = computeCartesianProduct(targetLists);

// 打印结果验证
for (List<String> combo : cartesianResult) {
    System.out.println(combo);
}

比如你给的示例中,curpage1list是["page1d", "page1d1"],curpage2list是["page2d", ...],运行后会输出像[page1d, page2d]、[page1d, page2d1]、[page1d1, page2d]这类所有可能的组合。

可选:泛型版本(支持任意类型)

如果你的集合里是Integer或者其他类型,可以把方法改成泛型,这样通用性更强:

public static <T> List<List<T>> computeCartesianProductGeneric(List<List<T>> inputLists) {
    List<List<T>> result = new ArrayList<>();
    if (inputLists.isEmpty()) {
        result.add(new ArrayList<>());
        return result;
    }
    List<T> firstList = inputLists.get(0);
    List<List<T>> restProduct = computeCartesianProductGeneric(inputLists.subList(1, inputLists.size()));
    for (T item : firstList) {
        for (List<T> combination : restProduct) {
            List<T> newCombination = new ArrayList<>();
            newCombination.add(item);
            newCombination.addAll(combination);
            result.add(newCombination);
        }
    }
    return result;
}

这样不管是处理List<List<Integer>>还是List<List<String>>都能用啦!

内容的提问来源于stack exchange,提问作者Garry Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:29:27