【问题标题】:How to I get aggregated object list from repeated fields of object collection with stream lambda如何使用流 lambda 从对象集合的重复字段中获取聚合对象列表
【发布时间】:2020-11-25 21:31:45
【问题描述】:
class ProductTitle {

    private Long productId;

    private String productName

    private String sellerName

    private Long categoryId;

    private Long titleId;

    private int status;

    private String title; 

    private Long titlePrice;

}

class Aggregate {
    productId;
    productName;
    sellerName;
    categoryId;
    List<TitleInfo> titles;
}

class TitleInfo {
    titleId;
    status;
    title;
}

SampleData:
productId:1, productname: "product1", sellerName: "seller1",categoryId:2, titleId:25, status:1, title:title1
productId:1, productname: "product1", sellerName: "seller1",categoryId:2, titleId:23, status:1, title:title2
productId:1, productname: "product1", sellerName: "seller1",categoryId:2, titleId:45, status:1, title:title3
productId:2, productname: "product2", sellerName: "seller2",categoryId:5, titleId:67, status:0, title:title4
productId:2, productname: "product2", sellerName: "seller2",categoryId:5, titleId:11, status:1, title:title5
productId:3, productname: "product3", sellerName: "seller3",categoryId:9, titleId:10, status:1, title:title6
productId:6, productname: "product4", sellerName: "seller4",categoryId:7, titleId:36, status:1, title:title7

我有像 sampleData 一样的 ProductTitle 列表。列表中的某些项目重复了 SellerName、productName、categoryId 和 productId。我需要像List&lt;Aggregate&gt; list 这样输出。如何通过流获得此结果。

对不起我的英语

【问题讨论】:

  • 到目前为止你尝试了什么?
  • @Naman 我尝试对重复字段进行分组,但它看起来像这样Map&lt;Long, Map&lt;Long,Map&lt;String, Map&lt;String,List&lt; ProductTitle &gt;&gt;&gt;&gt;&gt;我不确定这是正确的方法
  • 您可以查看this answer 中的groupingBy 方法并定义产品标题到标题信息的值映射。如果您尝试这样做并且中途卡住,请使用您的解决方案编辑问题并询问什么不起作用。

标签: java java-8 java-stream grouping


【解决方案1】:

我首先将groupingBy 方法与mapping 收集器一起用于值,然后将map 生成的映射到您想要的对象。

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

import java.util.List;
import java.util.stream.Stream;

class x
{
    public static void main(String[] args) {
        Stream<ProductTitle> sampleData= Stream.of(
            new ProductTitle(1, "product1", "seller1", 2, 25, 1, "title1"),
            new ProductTitle(1, "product1", "seller1", 2, 23, 1, "title2"),
            new ProductTitle(1, "product1", "seller1", 2, 45, 1, "title3"),
            new ProductTitle(2, "product2", "seller2", 5, 67, 0, "title4"),
            new ProductTitle(2, "product2", "seller2", 5, 11, 1, "title5"),
            new ProductTitle(3, "product3", "seller3", 9, 10, 1, "title6"),
            new ProductTitle(6, "product4", "seller4", 7, 36, 1, "title7"));

        List<Aggregate>
                aggregateKeyListMap =
                sampleData.collect(collectingAndThen(groupingBy(ProductTitle::toKey, mapping(ProductTitle::toTitleInfo, toList())),
                        groupedByKey -> groupedByKey.entrySet()
                                .stream()
                                .map(entry -> new Aggregate(entry.getKey(), entry.getValue()))
                                .collect(toList())));
        aggregateKeyListMap.forEach(System.out::println);
    }

    static class ProductTitle
    {
        private Long   productId;
        private String productName;
        private String sellerName;
        private Long   categoryId;
        private Long   titleId;
        private int    status;
        private String title;
        private Long   titlePrice;

        public ProductTitle(long productId, String productName, String sellerName, long categoryId, long titleId, int status, String title) {
            this.productId = productId;
            this.productName = productName;
            this.sellerName = sellerName;
            this.categoryId = categoryId;
            this.titleId = titleId;
            this.status = status;
            this.title = title;
        }

        public AggregateKey toKey() {
            return new AggregateKey(productId, productName, sellerName, categoryId);
        }

        public TitleInfo toTitleInfo() {
            return new TitleInfo(titleId, status, title);
        }
    }

    @lombok.ToString(includeFieldNames = false)
    @lombok.EqualsAndHashCode
    @lombok.AllArgsConstructor
    static class AggregateKey
    {
        Long   productId;
        String productName;
        String sellerName;
        Long   categoryId;
    }

    @lombok.ToString(includeFieldNames = false)
    @lombok.AllArgsConstructor
    static class Aggregate
    {
        AggregateKey        key;
        List<TitleInfo> titles;
    }

    @lombok.ToString(includeFieldNames = false)
    @lombok.AllArgsConstructor
    static class TitleInfo
    {
        Long   titleId;
        int    status;
        String title;
    }

}

对于示例数据,它正在产生:

x.Aggregate(x.AggregateKey(6, product4, seller4, 7), [x.TitleInfo(36, 1, title7)])
x.Aggregate(x.AggregateKey(1, product1, seller1, 2), [x.TitleInfo(25, 1, title1), x.TitleInfo(23, 1, title2), x.TitleInfo(45, 1, title3)])
x.Aggregate(x.AggregateKey(3, product3, seller3, 9), [x.TitleInfo(10, 1, title6)])
x.Aggregate(x.AggregateKey(2, product2, seller2, 5), [x.TitleInfo(67, 0, title4), x.TitleInfo(11, 1, title5)])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多