java stream 如何将相同的值合并到列表中

java stream How to merge same value to list

我的实体是

private String subject;
private String unit1;  
private String unit2;  
private String unit3;  

List<entity> getList = repo.findAll();

"getList" 显示此列表 我的控制器 return 这个值

 [
        {
          "unit1": "wert",
          "unit2": "2",
          "unit3": "6",
          "subject": "grdg"
        },
        {
          "unit1": "sdfg",
          "unit2": "2",
          "unit3": "e",
          "subject": "gdsg4"
        },
        {
          "unit1": "sdfg",
          "unit2": "3",
          "unit3": "hrh",
          "subject": "g4ds"
        },
        {
          "unit1": "qwer",
          "unit2": "4",
          "unit3": "rh5",
          "subject": "g4e"
        },
        {
          "unit1": "asdf",
          "unit2": "5",
          "unit3": "erty",
          "subject": "asdf"
        },
    {
      "unit1": "zxcv",
      "unit2": "2",
      "unit3": "3",
      "subject": "asdf"
    }
  ]

我想合并同一个主题

{asdf:[{"unit1": "wert",
      "unit2": "2",
      "unit3": "6"},{ "unit1": "zxcv",
      "unit2": "2",
      "unit3": "3"}],
g4e:[{
          "unit1": "qwer",
          "unit2": "4",
          "unit3": "rh5"}]
        }
}

我是说我想这样合并

{ subject_name:[other_data]}

getList.stream.collect(Collectors.groupingBy(Subject::getSubject)).values().stream().collect(Collectors.toList());

那个代码合并是对的?

groupBy 的足够代码是 不从分组 Map 中提取值 ,这样您就剩下每个主题与其对应 [=14] 的映射=] 个实体。

Map<String, List<Subject>> subjectMap = getList.stream()
        .collect(Collectors.groupingBy(Subject::getSubject));

此外,当你想将List<Subject>的元素映射到另一种视图类型时,你可以执行映射操作为:

Map<String, List<Wrapper>> subjectMap = getList.stream()
        .collect(Collectors.groupingBy(Subject::getSubject,
                Collectors.mapping(Wrapper::extractAttributes,
                        Collectors.toList())));

附加 class 定义为(或者您也可以选择改变现有的 Subject class):

@Builder
static class Wrapper {
    private String unit1;
    private String unit2;
    private String unit3;

    public static Wrapper extractAttributes(Subject subject) {
        return Wrapper.builder().unit1(subject.getUnit1())
                .unit2(subject.getUnit2())
                .unit3(subject.getUnit3()).build();
    }
}

这个怎么样?

 asdf: [
                {
                    "unit1":
                        [{
                            "getdata": [{
                                "unit2": "asdff",
                                "unit3": "aaa"
                            }, {
                                "unit2": "3sfa",
                                "unit3": "ff"
                            }, {
                                "unit2":
                                    [{
                                        "unit2_va": [
                                            {"unit3": "aaa"},
                                            {"unit3": "ccc"}
                                        ]

                                    }]
                            }]
                        }
                        ]
                }]

也是这样吗?