代码之家  ›  专栏  ›  技术社区  ›  baao

流创建一个附加列表-如何避免

  •  2
  • baao  · 技术社区  · 7 年前

    流如下所示

      public List<List<CalculatedItemCostPerStore>> getCalcualtedItemCostPerStore() {
        var it = item
            .getQuantities().stream().map(quantities -> {
              Store.StoreCosts<CostType, BigDecimal> storeCosts = quantities.getStore().getStoreCosts();
              return new CalculatedItemCostPerStore(quantities.getStore().getName(), new TreeSet<>(quantities
                  .getItemCostEvents()
                  .stream()
                  .map(e -> new CalculatedItemCost(e.getCostTrigger(), e.getTriggerQuantity().multiply(storeCosts.get(e.getCostTrigger()))))
                  .collect(
                      Collectors.toMap(CalculatedItemCost::getCostType, Function.identity(), (a, b) -> new CalculatedItemCost(a.getCostType(), a.getCost().add(b.getCost())))
                  ).values()));
            }).collect(Collectors.groupingBy(CalculatedItemCostPerStore::getStore)).values().stream().collect(Collectors.toList());
        return it;
      }
    

    被流到

      @Data
      @AllArgsConstructor
      static class CalculatedItemCost implements Comparable<CalculatedItemCost> {
        private CostType costType;
        private BigDecimal cost;
    
        @Override
        public int compareTo(CalculatedItemCost o) {
          return this.costType.toString().compareTo(o.getCostType().toString());
        }
      }
    
      @Data
      @AllArgsConstructor
      static class CalculatedItemCostPerStore {
        private String store;
        private TreeSet<CalculatedItemCost> calculatedItemCosts;
      }
    

    itemCostEvents是

    {
      "createdAt": "2018-08-29T00:00:00",
      "costTrigger": "STORAGE_COST_PER_CBM_PER_DAY",
      "triggerQuantity": 33
    }
    

    上面的流创建一个由下面的json表示的结果。正如你所看到的,这是一个 List<List<CalculatedItemCostPerStore>>

    "calcualtedItemCostPerStore": [
        [
          {
            "store": "foo",
            "calculatedItemCosts": [
              {
                "costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
                "cost": 1368
              },
              {
                "costType": "STORAGE_COST_PER_CBM_PER_DAY",
                "cost": 287.1
              }
            ]
          }
        ],
        [
          {
            "store": "bar",
            "calculatedItemCosts": [
              {
                "costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
                "cost": 38
              }
            ]
          }
        ]
      ]
    

    数量可以有任意数量的出现,其中每个都可以有一个存储。

    我想要的是一张单人票 List<CalculatedItemCostPerShop> 如下图所示

    "calcualtedItemCostPerStore": [
        {
            "store": "foo",
            "calculatedItemCosts": [
                {
                    "costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
                    "cost": 1368
                },
                {
                    "costType": "STORAGE_COST_PER_CBM_PER_DAY",
                    "cost": 287.1
                }
            ]
        },
        {
            "store": "bar",
            "calculatedItemCosts": [
                {
                    "costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
                    "cost": 38
                }
            ]
        }
     ]
    

    我需要如何调整我的流以获得上述结果?

    1 回复  |  直到 7 年前
        1
  •  1
  •   baao    7 年前

    多亏了@ernest\u k-using解决了这个问题 flatMap .values()

    public List<CalculatedItemCostPerStore> getCalcualtedItemCostPerStore() {
        return item
            .getQuantities().stream().map(quantities -> {
              Store.StoreCosts<CostType, BigDecimal> storeCosts = quantities.getStore().getStoreCosts();
              return new CalculatedItemCostPerStore(quantities.getStore().getName(), new TreeSet<>(quantities
                  .getItemCostEvents()
                  .stream()
                  .map(e -> new CalculatedItemCost(e.getCostTrigger(), e.getTriggerQuantity().multiply(storeCosts.get(e.getCostTrigger()))))
                  .collect(
                      Collectors.toMap(CalculatedItemCost::getCostType, Function.identity(), (a, b) -> new CalculatedItemCost(a.getCostType(), a.getCost().add(b.getCost())))
                  ).values()));
            })
            .collect(Collectors.groupingBy(CalculatedItemCostPerStore::getStore))
            .values()
            .stream()
            .flatMap(Collection::stream).collect(Collectors.toList())
            ;
      }
    
    推荐文章