java—如何使用流lambda从对象集合的重复字段中获取聚合对象列表

vxqlmq5t  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(473)
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。列表中的某些项重复了sellername、productname、categoryid和productid。我需要像这样输出 List<Aggregate> list . 如何用stream得到这个结果。
对不起我的英语

atmip9wb

atmip9wb1#

我先用 groupingBy 接近 mapping 值的收集器,然后 map 结果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)])

相关问题