java 8 sum field from list of object with common

我有这个对象列表


List<DetailDto> details;

    @Value

    public class DetailDto implements Serializable {

        String category1;

        Integer category2;

        Integer price;


        transient Integer totalPrice;

    }

有了这份清单


[

  {

    "category1": "ABC",

    "category2": 30,

    "price": 195,

    "totalPrice": null

  },

  {

    "category1": "ABC",

    "category2": 30,

    "price": 195,

    "totalPrice": null

  },

  {

    "category1": "ABC",

    "category2": 30,

    "price": 195,

    "totalPrice": null

  },

  {

    "category1": "ABC",

    "category2": 30,

    "price": 390,

    "totalPrice": null

  },

  {

    "category1": "ABC",

    "category2": 30,

    "price": 390,

    "totalPrice": null

  },

  {

    "category1": "DEF",

    "category2": 30,

    "price": 455,

    "totalPrice": null

  },

  {

    "category1": "DEF",

    "category2": 30,

    "price": 455,

    "totalPrice": null

  },

  {

    "category1": "DEF",

    "category2": 30,

    "price": 455,

    "totalPrice": null

  },

  {

    "category1": "DEF",

    "category2": 30,

    "price": 455,

    "totalPrice": null

  },

  {

    "category1": "GHI",

    "category2": 1,

    "price": 18000,

    "totalPrice": null

  }

]

我想通过将字段List<DetailDto>总结为条件为的字段来制作另一个对象:pricetotalPrice


字符串category1很常见

整数category2很常见

整数price很常见

在这一点上我有这个


List<List<DetailDto>> summarizedList = detail().stream()

        .collect(Collectors.groupingBy(DetailDto::category1,

                                       Collectors.groupingBy(DetailDto::category2,

                                                             Collectors.groupingBy(DetailDto::price))))

        .values()

        .stream()

        .flatMap(c1 -> c1.values().stream())

        .flatMap(c2 -> c2.values().stream())

        .collect(Collectors.toList());

返回我List<List<DetailDto>>


而且我不知道如何使它正确,在我尝试这个之后


summarizedList.stream().map(dto -> dto.stream().reduce((x,y) -> new DetailDto(x.productCode(), x.productQt(), x.orderPrice(), Integer.sum(x.orderPrice(), y.orderPrice()).orElse(null).collect(Collectors.toList());

你们能帮帮我吗?


尚方宝剑之说
浏览 81回答 1
1回答

白猪掌柜的

这是一种可能性:使用复合键一次分组:.collect(Collectors.groupingBy(DetailDto::key, Collectors.summarizingInt(DetailDto::getPrice)))请参阅KeyDetailDto.java 中的定义(并注意其 eclipse 生成hashCode的equals方法):import java.io.Serializable;@SuppressWarnings("serial")public class DetailDto implements Serializable {&nbsp; &nbsp; String category1;&nbsp; &nbsp; Integer category2;&nbsp; &nbsp; Integer price;&nbsp; &nbsp; transient Integer totalPrice;&nbsp; &nbsp; public DetailDto() {&nbsp; &nbsp; }&nbsp; &nbsp; public DetailDto(String category1, Integer category2, Integer price, Integer totalPrice) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.category1 = category1;&nbsp; &nbsp; &nbsp; &nbsp; this.category2 = category2;&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;&nbsp; &nbsp; &nbsp; &nbsp; this.totalPrice = totalPrice;&nbsp; &nbsp; }&nbsp; &nbsp; public String getCategory1() {&nbsp; &nbsp; &nbsp; &nbsp; return category1;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCategory1(String category1) {&nbsp; &nbsp; &nbsp; &nbsp; this.category1 = category1;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getCategory2() {&nbsp; &nbsp; &nbsp; &nbsp; return category2;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCategory2(Integer category2) {&nbsp; &nbsp; &nbsp; &nbsp; this.category2 = category2;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getPrice() {&nbsp; &nbsp; &nbsp; &nbsp; return price;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPrice(Integer price) {&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getTotalPrice() {&nbsp; &nbsp; &nbsp; &nbsp; return totalPrice;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTotalPrice(Integer totalPrice) {&nbsp; &nbsp; &nbsp; &nbsp; this.totalPrice = totalPrice;&nbsp; &nbsp; }&nbsp; &nbsp; Key key() {&nbsp; &nbsp; &nbsp; &nbsp; return new Key(category1, category2, price);&nbsp; &nbsp; }}class Key {&nbsp; &nbsp; String category1;&nbsp; &nbsp; Integer category2;&nbsp; &nbsp; Integer price;&nbsp; &nbsp; public Key(String category1, Integer category2, Integer price) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.category1 = category1;&nbsp; &nbsp; &nbsp; &nbsp; this.category2 = category2;&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;&nbsp; &nbsp; }&nbsp; &nbsp; public String getCategory1() {&nbsp; &nbsp; &nbsp; &nbsp; return category1;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getCategory2() {&nbsp; &nbsp; &nbsp; &nbsp; return category2;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getPrice() {&nbsp; &nbsp; &nbsp; &nbsp; return price;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; final int prime = 31;&nbsp; &nbsp; &nbsp; &nbsp; int result = 1;&nbsp; &nbsp; &nbsp; &nbsp; result = prime * result + ((category1 == null) ? 0 : category1.hashCode());&nbsp; &nbsp; &nbsp; &nbsp; result = prime * result + ((category2 == null) ? 0 : category2.hashCode());&nbsp; &nbsp; &nbsp; &nbsp; result = prime * result + ((price == null) ? 0 : price.hashCode());&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object obj) {&nbsp; &nbsp; &nbsp; &nbsp; if (this == obj)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; if (obj == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (getClass() != obj.getClass())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; Key other = (Key) obj;&nbsp; &nbsp; &nbsp; &nbsp; if (category1 == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (other.category1 != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; } else if (!category1.equals(other.category1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (category2 == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (other.category2 != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; } else if (!category2.equals(other.category2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (price == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (other.price != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; } else if (!price.equals(other.price))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}Main.javaimport java.io.IOException;import java.util.IntSummaryStatistics;import java.util.Map;import java.util.stream.Collectors;import java.util.stream.Stream;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.ObjectWriter;public class Main {&nbsp; &nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; DetailDto[] values = new ObjectMapper().readerFor(DetailDto[].class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .readValue(Main.class.getResourceAsStream("data.json"));//&nbsp; &nbsp; &nbsp; for (DetailDto dto : values) {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display(dto);//&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Map<Key, IntSummaryStatistics> res = Stream.of(values)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(DetailDto::key, Collectors.summarizingInt(DetailDto::getPrice)));&nbsp; &nbsp; &nbsp; &nbsp; Stream<DetailDto> agg = res.entrySet().stream().map(e -> new DetailDto(e.getKey().category1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.getKey().category2, e.getKey().price, (int) e.getValue().getSum()));&nbsp; &nbsp; &nbsp; &nbsp; agg.forEach(Main::display);&nbsp; &nbsp; }&nbsp; &nbsp; protected static void display(DetailDto dto) {&nbsp; &nbsp; &nbsp; &nbsp; final ObjectWriter json = new ObjectMapper().writerFor(DetailDto.class).withDefaultPrettyPrinter();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(json.writeValueAsString(dto));&nbsp; &nbsp; &nbsp; &nbsp; } catch (JsonProcessingException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java