lambda - 提取属性模式,使用 java lambda 合并并构建列表

标签 lambda java-8

我不是 Java Lambda 方面的专家,所以我需要您的帮助。

如何提取以下列表:

[
    {
      "code": 1,
      "nestedList": [{
        "value": "A",
        "count": 1
      }]
    },
    {
      "code": 2,
      "nestedList": [{
        "value": "A",
        "count": 1
      }]
    },
    {
      "code": 3,
      "nestedList": [{
        "value": "A",
        "count": 1
      }]
    },
    {
      "code": 4,
      "nestedList": [{
        "value": "B",
        "count": 1
      }]
    },
    {
      "code": 5,
      "nestedList": [{
        "value": "B",
        "count": 1
      }]
]

至以下一个:

[
  {
    "value": "A",
    "count": 3
  },
  {
    "value": "B",
    "count": 2
  }
]

我尝试了groupingBy、filter等操作,但没有任何效果......

因为我使用以下代码:

public class MainObject {
    List<NestedObject> nestedList;
    //getters and setters
}

public class NestedObject {
    private String value;
    private Integer count;
    //getters and setters
}

List<MainObject> mainObjectList = new ArrayList<>();
        List<NestedObject> nestedObjects = new ArrayList<>();
        NestedObject nestedObject = new NestedObject();
        nestedObject.setValue("A");
        nestedObject.setCount(3);
        nestedObjects.add(nestedObject);
        MainObject mainObject = new MainObject();
        mainObject.setNestedList(nestedObjects);
        mainObjectList.add(mainObject);

        List<NestedObject> nestedList2 = new ArrayList<>();
        NestedObject nestedObject2 = new NestedObject();
        nestedObject2.setValue("A");
        nestedObject2.setCount(2);
        nestedList2.add(nestedObject2);
        MainObject mainObject2 = new MainObject();
        mainObject2.setNestedList(nestedList2);
        mainObjectList.add(mainObject2);

        List<NestedObject> nestedList3 = new ArrayList<>();
        NestedObject nestedObject3 = new NestedObject();
        nestedObject3.setValue("B");
        nestedObject3.setCount(8);
        nestedList3.add(nestedObject3);
        MainObject mainObject3 = new MainObject();
        mainObject3.setNestedList(nestedList3);
        mainObjectList.add(mainObject3);

        List<NestedObject> nestedList4 = new ArrayList<>();
        NestedObject nestedObject4 = new NestedObject();
        nestedObject4.setValue("B");
        nestedObject4.setCount(2);
        nestedList4.add(nestedObject4);
        MainObject mainObject4 = new MainObject();
        mainObject4.setNestedList(nestedList4);
        mainObjectList.add(mainObject4);

        System.out.println(mainObjectList.size());

        List<String> listInteger =
                mainObjectList.stream()
                .map(mainObject -> mainObject.getNestedList())
                .flatMap(List::stream)
                .map(NestedObject::getValue)
                .collect(NestedObject::getValue);

我试图提取,然后使用另一个foreach获取计数

我搜索了类似 underscore.js 的内容。但我真的不知道....

Summarize array values, underscore

最佳答案

从预期结果来看,不清楚是否要添加分组后同一存储桶中所有对象的 count 属性,还是仅添加分组后每个存储桶中元素的计数。

因此,我将提供几种解决方案,您可以选择您想要的解决方案。

如果您想按分组,则对同一存储桶中所有对象的count属性求和:

Map<String, Integer> resultSet = mainObjectList.stream()
                .map(MainObject::getNestedList)
                .flatMap(List::stream)
                .collect(Collectors.groupingBy(NestedObject::getValue,
                        Collectors.summingInt(NestedObject::getCount)));

或者如果您想将结果收集回列表中累积的 NestedObject 类型:

List<NestedObject> resulSet = mainObjectList.stream()
                .map(MainObject::getNestedList)
                .flatMap(List::stream)
                .collect(Collectors.groupingBy(NestedObject::getValue,
                        Collectors.summingInt(NestedObject::getCount)))
                .entrySet()
                .stream()
                .map(e -> {
                    NestedObject obj = new NestedObject();
                    obj.setCount(e.getValue());
                    obj.setValue(e.getKey());
                    return obj;
                }).collect(Collectors.toList());

如果您想按分组,请计算同一存储桶中的对象数量:

Map<String, Long> resultSet = mainObjectList.stream()
            .map(MainObject::getNestedList)
            .flatMap(List::stream)
            .collect(Collectors.groupingBy(NestedObject::getValue,
                    Collectors.counting()));

或者如果您想将结果收集回列表中累积的 NestedObject 类型:

List<NestedObject> resultSet = mainObjectList.stream()
                .map(MainObject::getNestedList)
                .flatMap(List::stream)
                .collect(Collectors.groupingBy(NestedObject::getValue,
                        Collectors.summingInt(e -> 1)))
                .entrySet()
                .stream()
                .map(e -> {
                    NestedObject obj = new NestedObject();
                    obj.setCount(e.getValue());
                    obj.setValue(e.getKey());
                    return obj;
                }).collect(Collectors.toList());

关于lambda - 提取属性模式,使用 java lambda 合并并构建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806812/

相关文章:

c++ - 使用 noexcept 作为 lambda 修饰符或参数约束

Python:使用 lambda 以 str 格式对日期时间进行排序

java - 如何将以下方法转换为 Java 8 内联函数?

java-8 - 如何使用 Java8 Date API 将 "December 15, 2009"解析为 Date?

java - 如何使用流 API 将两个过滤器应用于一个列表

c# - 使用 lambda 对多列进行分组

lambda - 尝试在业务域和数据域之间转换表达式树是否可行?

c++ - 你能在 lambda 中捕获数组吗?

java - Kotlin 中 getOrElseReturn 的可能性

java-8 - 用替换方法和Java 8替换大数组中的部分字符串