Java List<Bean> 分组转换

1. 什么是List<Bean> 分组转换, 请看如下数据结构,getter and setter 方法省略

 

    待分组数据结构

public class CommuneResourceBean implements java.io.Serializable {
    private Integer id;
    private Integer productId;
    private String productName;
    private Date priceDate;
    private double cashRebate;
    private Double communePrice;
    private Double communeMinus;

}

    分组数据结构

public class CommuneResourceV4 implements java.io.Serializable{
    private String date;
    private List<CommuneResourceV4PriceBean> prices;

}

public class CommuneResourceV4PriceBean implements java.io.Serializable{
     private String productName;
    private double cashRebate;
    private Double communePrice;
    private Double communeMinus;

}

现在将List<CommuneResourceBean> list转换成List<CommuneResourceV4> v4list 的数据结构,  分组依据是priceDate

        if(list.size()>0){
    		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    		for(CommuneResourceBean asm : list){
    			Date priceDate = asm.getPriceDate();
	    		String priceDateStr = df.format(priceDate);
	    		int index = containDate(v4list,priceDateStr);
	    		CommuneResourceV4PriceBean bean = new CommuneResourceV4PriceBean();
	    		String simpleProductName = productName;
	                
                        bean.setProductName(simpleProductName);
    			bean.setCashRebate(asm.getCashRebate());
    			bean.setCommunePrice(asm.getCommunePrice());
    			bean.setCommuneMinus(asm.getCommuneMinus());
	    		if(index>=0){
	    			CommuneResourceV4 v4 = v4list.get(index);
	    			List<CommuneResourceV4PriceBean> priceBeanList = v4.getCourses();
	    			priceBeanList.add(bean);
	    		}else{
	    			CommuneResourceV4 v4 = new CommuneResourceV4();
	    			List<CommuneResourceV4PriceBean> priceBeanList = new ArrayList<CommuneResourceV4PriceBean>();
	    			priceBeanList.add(bean);
	    			v4.setDate(priceDateStr);
	    			v4.setCourses(priceBeanList);
	    			v4list.add(v4);
	    		}
	    		
    		}
    		
        }
        return v4list;

判断函数


    /**
     * 判断某个 List<CommuneResourceV4> v4list  是否包含CommuneResourceV4.priceDate为priceDateStr的元素
     * @param priceDateStr 
     * @param v4list 
     * @param v4list
     * @param priceDate
     * @return index 代表 list表中所在的位置,默认-1
     */
    private int containDate(List<CommuneResourceV4> v4list, String priceDateStr) {
		int index=-1;
    	if(v4list.size()>0){
			//如果v4list包含日起为priceDate的数据  将asm 写入
	    	for(CommuneResourceV4 v4 : v4list){
				index++;
				if(v4.getDate().equals(priceDateStr)){
					return index;
				}
			}
	    	return -1;
		}else{
			return index;
		}
    	
	}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值