我有很多评估我想要收集数据,我一直试图弄清楚如何用流来实现这一目标。我特别关注流,因为集合大小可能相当大,我想利用并行处理。
以下是一些示例代码,它反映了一个基本结构 -
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
类应该在链的末尾完全填充同样,尝试使用嵌套逻辑执行此操作的原因是因为我想要使用并行性调用。如果您对如何最好地完成任何建议,我可以使用该指南。