链/嵌套java流

时间:2018-02-12 03:42:19

标签: java java-8 java-stream chaining

我有很多评估我想要收集数据,我一直试图弄清楚如何用流来实现这一目标。我特别关注流,因为集合大小可能相当大,我想利用并行处理。

以下是一些示例代码,它反映了一个基本结构 -

import java.math.BigDecimal;
import java.util.Map;
import java.util.List;

public class TestApplication {

  /***
   * Enumerated list of various product types
   ***/
  public enum ProductType {
    TOY, BATH,  BEDROOM, OUTDOOR
  }

  /***
   * Represents each product we have in inventory.
   ***/
  class Product {
    public String name;
    public ProductType type;
    public int quantity = 0;
    public double costPerUnit = 0;
    public double productValue = 0;
    public float percentOfTotalInventory;

    public void setName(String name) { this.name = name; }
    public void setProductType(ProductType type) { this.type = type; }
    public void setQuantity(int quantity) { this.quantity = quantity; }
    public void setCostPerUnit(double unitCost) { this.costPerUnit = unitCost; }

    public void calculateProductValue() {
      this.productValue = quantity * costPerUnit;
    }

    public void calculatePercentageOfTotalInventory(double totalInventoryValue) {
      this.percentOfTotalInventory = (float) ((costPerUnit * 100) / totalInventoryValue);
    }

    public String getName() { return name; }
    public ProductType getType() { return type; }
    public int getQuantity() { return quantity; }
    public double getCostPerUnit() { return costPerUnit; }
    public float getPercentOfTotalInventory() { return percentOfTotalInventory; }
  }

  /***
   * summary of all the products in our inventory.
   *
   * in addition to the catalog of products, will keep a running total cost of
   * the current inventory and a summary grouping of the products by type
   ***/
  class ProductInventory {
        public List<Product> products;
        public BigDecimal totalCostOfInventory;
        public Map<ProductType, List<ProductSummaryByType>> productsByType;

      public void accept(Product p) {
         p.calculateProductValue();
         products.add(p);
         totalCostOfInventory = totalCostOfInventory.add(BigDecimal.valueOf(p.getCostPerUnit()));
       }

      public void combine(ProductInventory other) {
         this.products.addAll(other.products);
         this.totalCostOfInventory = totalCostOfInventory.add(other.totalCostOfInventory);
      }
  }

  private class ProductSummaryByType {
    //for a type (toy, bath, bed)
    public ProductType Type;

    //provide total value of inventory for this product type
    public double value;
    public float percentageOfTotal;

    public void calcPercentOfTotal(double total) {
        //given the typeValuation, calculate percentage of the total inventory is this type?
        this.percentageOfTotal = (float) ((this.value * 100) / total);
    }
  }

 private Map<String, BigDecimal> getProductPrice(String productName) {
   //get current price for product
   Map<String, BigDecimal> productInfo = new HashMap<String, BigDecimal>();
   //simulate the pricing
   productInfo.put(productName, BigDecimal.ONE);
   return productInfo;
 }

  public void run(String... args) throws Exception {
    //provide product list and process stream
  }
}

对于产品系列,每个产品每天都会有不同的购买价格,因此设置costPerUnit有不同的例程。

我期待:

  • 遍历产品列表并捕获产品名称的集合
    • 例如:List<String> productNames = productList.stream().map(Product::getName).collect(Collectors.toList());
  • 将名称映射到函数以获取当前价格
    • 例如:productNames.stream().map(this::getProductPrice).collect(Collectors.toList());
  • 对于更新价目表中的每个项目,为产品清单集合中的每个匹配实例设置costPerUnit
  • 对于产品的每个实例,计算每个实例的成本(calculateProductValue)并添加到ProductInventory,我假设为自定义收集器,因此它可以计算所有库存的总成本
  • 以及所有广告资源的总费用,通过调用percentOfTotalInventory
  • 计算每个产品的calculatePercentageOfTotalInventory
  • 最后,按ProductType将广告资源与该组的总值进行分组(为该组中的每个实例添加productValue),并在ProductSummaryByType中计算该组的percentageOfTotal
  • ProductInventory类应该在链的末尾完全填充

同样,尝试使用嵌套逻辑执行此操作的原因是因为我想要使用并行性调用。如果您对如何最好地完成任何建议,我可以使用该指南。

0 个答案:

没有答案