链/嵌套java流
我希望对一组数据进行一些评估,我一直在试图找出如何通过流来实现这一点。我特别关注流,因为集合的大小可能相当大,我希望利用并行处理 下面是一些示例代码,它反映了一个基本结构-链/嵌套java流,java,java-8,java-stream,chaining,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
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
}
}
import java.math.BigDecimal;
导入java.util.Map;
导入java.util.List;
公共类测试应用程序{
/***
*各种产品类型的枚举列表
***/
公共枚举产品类型{
玩具、浴室、卧室、户外
}
/***
*表示库存中的每种产品。
***/
类产品{
公共字符串名称;
公共产品类型;
公共整数数量=0;
公共双成本单位=0;
公共双乘积值=0;
总库存的公众浮动百分比;
public void setName(字符串名){this.name=name;}
public void setProductType(ProductType类型){this.type=type;}
public void setQuantity(int-quantity){this.quantity=quantity;}
public void setCostPerUnit(双重单位成本){this.costPerUnit=unitCost;}
public void calculateProductValue(){
this.productValue=数量*成本单位;
}
总存货的公共作废计算百分比(总存货价值的两倍){
总库存百分比=(浮动)((成本单位*100)/总库存价值);
}
公共字符串getName(){return name;}
public ProductType getType(){return type;}
public int getQuantity(){返回数量;}
public double getCostPerUnit(){return costPerUnit;}
公共浮点getPercentOfTotalInventory(){return percentOfTotalInventory;}
}
/***
*我们库存中所有产品的汇总。
*
*除了产品目录外,还将保持运行总成本为
*当前库存和按类型对产品的汇总分组
***/
类别产品目录{
公开上市产品;
存货的公共成本;
公共地图产品类型;
公共无效接受(产品p){
p、 calculateProductValue();
产品.加入(p);;
totalCostOfInventory=totalCostOfInventory.add(BigDecimal.valueOf(p.getCostPerUnit());
}
公共无效联合收割机(产品库存其他){
此.products.addAll(其他.products);
this.totalCostOfInventory=totalCostOfInventory.add(other.totalCostOfInventory);
}
}
私有类ProductSummaryByType{
//a型(玩具、浴缸、床)
公共产品类型;
//提供此产品类型的库存总值
公共双重价值;
公众持股占总股本的百分比;
公共无效计算总额(双倍总额){
//给定类型估值,计算总库存的百分比是否为该类型?
this.percentageOfTotal=(浮动)((this.value*100)/总计);
}
}
私有地图getProductPrice(字符串产品名){
//获取产品的当前价格
Map productInfo=new HashMap();
//模拟定价
productInfo.put(productName,BigDecimal.ONE);
返回productInfo;
}
公共无效运行(字符串…参数)引发异常{
//提供产品清单和工艺流程
}
}
对于一系列产品,每种产品每天都有不同的购买价格,因此有不同的例行程序来设置成本单位
我希望:
- 迭代产品列表并捕获产品名称的集合
- e、 g:
listProductNames=productList.stream().map(Product::getName).collect(Collectors.toList())代码>
- e、 g:
- 将名称映射到函数以获取当前价格
- e、 g:
productNames.stream().map(this::getProductPrice).collect(Collectors.toList())代码>
- e、 g:
- 对于更新的价目表中的每个项目,为产品库存集合中的每个匹配实例设置
costPerUnit
- 对于产品的每个实例,计算每个实例上的成本(
),并将其添加到calculateProductValue
,我假设它是一个自定义收集器,因此它可以计算所有库存的总成本ProductInventory
- 对于所有库存的总成本,通过调用总库存的
calculatePercentageOfTotalInventory
- 最后,按
将库存与该组的总值进行分组(为组中的每个实例添加ProductType
),并计算ProductSummaryByType类中该组的productValue
percentageOfTotal
类应在链的末尾完全填充ProductInventory
同样,尝试使用嵌套逻辑执行此操作的原因是因为我希望使用并行调用。如果您对如何最好地完成任务有任何建议,我可以使用指南。多大是“相当大?”超过100000个元素?超过一百万?数据库真的是更好的主意吗?目前不超过30k。我已经评估了db路由,只是想了解如何在代码中将逻辑链接在一起。你应该尝试一些东西,而不是写下要求。使用循环编写它,那么流部分应该很容易,如果可能的话