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

如何用Java Streams将Tuple列表转换为Mother对象列表

将Tuple列表映射为Mother类的实现方案

给定类定义

首先明确涉及的三个类结构:

class Tuple {
    String key;
    String key_key;
    String key_value;

    // 实际使用时需补充构造方法与getter
    public Tuple(String key, String key_key, String key_value) {
        this.key = key;
        this.key_key = key_key;
        this.key_value = key_value;
    }

    public String getKey() { return key; }
    public String getKey_key() { return key_key; }
    public String getKey_value() { return key_value; }
}

class Mother {
    String key;
    List<Son> sons;

    public Mother(String key, List<Son> sons) {
        this.key = key;
        this.sons = sons;
    }

    // 可按需补充getter/setter
}

class Son {
    String key;
    String value;

    public Son(String key, String value) {
        this.key = key;
        this.value = value;
    }

    // 可按需补充getter/setter
}

需求说明

需要将List<Tuple>按照Tuple.key分组,每组内的Tuple转换为Son对象(Son.key对应Tuple.key_key,Son.value对应Tuple.key_value),最终封装为List<Mother>。

比如输入数据:

k1, kk1, kv1
k1, kk1, kv2
k2, kk3, kv3

转换后得到:

  • Mother(key="k1", sons=[Son(key="kk1", value="kv1"), Son(key="kk1", value="kv2")])
  • Mother(key="k2", sons=[Son(key="kk3", value="kv3")])

实现代码

这里不需要用到flatMap(flatMap用于将流中元素拆分为多个元素展开,而我们的场景是分组聚合),核心用Collectors.groupingBy完成分组,再通过映射转换为目标对象:

import java.util.List;
import java.util.stream.Collectors;

public class TupleConverter {
    public static void main(String[] args) {
        // 模拟输入的Tuple列表
        List<Tuple> tuples = List.of(
                new Tuple("k1", "kk1", "kv1"),
                new Tuple("k1", "kk1", "kv2"),
                new Tuple("k2", "kk3", "kv3")
        );

        // 核心转换逻辑
        List<Mother> mothers = tuples.stream()
                // 按Tuple的key分组,得到key对应Tuple集合的Map
                .collect(Collectors.groupingBy(Tuple::getKey))
                // 遍历分组后的键值对,转换为Mother对象
                .entrySet().stream()
                .map(entry -> {
                    // 将当前分组的Tuple列表转为Son列表
                    List<Son> sonList = entry.getValue().stream()
                            .map(tuple -> new Son(tuple.getKey_key(), tuple.getKey_value()))
                            .collect(Collectors.toList());
                    return new Mother(entry.getKey(), sonList);
                })
                // 收集为Mother列表
                .collect(Collectors.toList());

        // 验证输出
        mothers.forEach(mother -> {
            System.out.printf("%s -> [", mother.key);
            mother.sons.forEach(son -> System.out.printf("[%s, %s], ", son.key, son.value));
            System.out.println("]");
        });
    }
}

代码解释

  1. Collectors.groupingBy(Tuple::getKey):将Tuple列表按key字段分组,得到Map<String, List<Tuple>>,键是Tuple的key,值是对应key的所有Tuple集合。
  2. 遍历Map entrySet:对每个分组的键值对进行转换,把分组内的Tuple逐个转成Son对象,再封装成Mother。
  3. Collectors.toList():最终将所有Mother对象收集为列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 23:54:30