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

基于id属性对Sample对象列表分组并提取value值

问题描述

现有Java类定义:

class Sample{
    String name;
    String value;
    String id;
}

输入的Sample对象列表为:

[Sample(name=title, value=Title, id=1), Sample(name=input, value=DCE, id=1), Sample(name=output, value=CDE, id=1), Sample(name=title, value=Title, id=2), Sample(name=input, value=DCEE, id=2), Sample(name=output, value=CDEE, id=2), Sample(name=title, value=Title, id=3), Sample(name=input, value=DCEEE, id=3), Sample(name=output, value=CDEEE, id=3)]

需按id属性对列表分组,最终仅展示每组内各对象的value值及对应id,支持以下两种输出格式:
格式1:

Title DCE CDE 1
Title DCEE CDEE 2
Title DCEEE CDEEE 3

格式2:

{[Title DCE CDE 1],
[Title DCEE CDEE 2],
[Title DCEEE CDEEE 3]}

实现方案(Java)

利用Java 8+ Stream API完成分组、排序与结果拼接,保证输出顺序符合title→input→output的预期:

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

public class SampleProcessor {
    public static void main(String[] args) {
        // 构造输入列表
        List<Sample> samples = List.of(
                new Sample("title", "Title", "1"),
                new Sample("input", "DCE", "1"),
                new Sample("output", "CDE", "1"),
                new Sample("title", "Title", "2"),
                new Sample("input", "DCEE", "2"),
                new Sample("output", "CDEE", "2"),
                new Sample("title", "Title", "3"),
                new Sample("input", "DCEEE", "3"),
                new Sample("output", "CDEEE", "3")
        );

        // 按id分组
        Map<String, List<Sample>> groupedById = samples.stream()
                .collect(Collectors.groupingBy(Sample::getId));

        // 输出格式1
        System.out.println("=== 格式1输出 ===");
        groupedById.forEach((id, sampleList) -> {
            String valueStr = sampleList.stream()
                    // 按指定顺序排序
                    .sorted((s1, s2) -> {
                        if (s1.name.equals("title")) return -1;
                        if (s1.name.equals("input")) return s2.name.equals("title") ? 1 : -1;
                        return 1;
                    })
                    .map(s -> s.value)
                    .collect(Collectors.joining(" "));
            System.out.println(valueStr + " " + id);
        });

        // 输出格式2
        System.out.println("\n=== 格式2输出 ===");
        String format2Result = groupedById.entrySet().stream()
                .map(entry -> {
                    String valueStr = entry.getValue().stream()
                            .sorted((s1, s2) -> {
                                if (s1.name.equals("title")) return -1;
                                if (s1.name.equals("input")) return s2.name.equals("title") ? 1 : -1;
                                return 1;
                            })
                            .map(s -> s.value)
                            .collect(Collectors.joining(" "));
                    return "[" + valueStr + " " + entry.getKey() + "]";
                })
                .collect(Collectors.joining(",\n"));
        System.out.println("{" + format2Result + "}");
    }

    // 补全Sample类的构造方法
    static class Sample {
        String name;
        String value;
        String id;

        public Sample(String name, String value, String id) {
            this.name = name;
            this.value = value;
            this.id = id;
        }
    }
}

运行结果

=== 格式1输出 ===
Title DCE CDE 1
Title DCEE CDEE 2
Title DCEEE CDEEE 3

=== 格式2输出 ===
{[Title DCE CDE 1],
[Title DCEE CDEE 2],
[Title DCEEE CDEEE 3]}

内容的提问来源于stack exchange,提问作者Sai Balaji MS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 19:15:34