实际场景,集合为商品订单集合,一个订单有多个商品,现在要做导出,商品有一个属性为订单总金额,如何得到这个订单总金额,并将其放入商品中?
//订单集合
List<Order> orderList =
new ArrayList<Order>();
//导出的列的key
String[] keys = {
"name",
"price",
"orderPrice"};
//导出的结果集,每一条记录就是一行,以k-v的方式存储
List<
Map<
String,
String>> exportList =
new ArrayList<
Map<
String,
String>>();
//假设现在订单集合里面有多个订单;
for(Order order : orderList){
//导出集合在添加一个订单数据前集合的长度
Integer beginIndex = exportList.size();
//订单中商品的集合
List<Product> productList = order.getProducts();
//用于记录订单总金额
Integer sum =
0;
for(Product product :productList){
Map<
String,
String> lineData =
new HashMap<
String,
String>();
//数据填充
lineData.put(
"name",product.getName());
lineData.put(
"price",product.getPrice()+
"");
sum += product.getPrice();
//将数据加入到导出的集合中
exportList.add(lineData);
}
//导出集合在添加一个订单数据后集合的长度
Integer endIndex = exportList.size();
//根据下标将刚刚放入集合中的对象进行遍历,为其填充订单总价这个数据
for(
int i=beginIndex;i<endIndex;i++){
Map<
String,
String> lineData = exportList.
get(i);
lineData.put(
"orderPrice",sum+
"");
}
}