list集合根据字段分组统计转换成map

前言

表格需要对数据进行统计

代码实现

    public Map getUnitStoreSum(String unitId, String billCode) {
        List store=listUnitStore(unitId, billCode);
        Map groupMap=new HashMap();
        for(int i=0;i<store.size();i++){        
            NoteStorageInfo nsi=(NoteStorageInfo) store.get(i); 
            List tempList=(List) groupMap.get(nsi.getPjmc());
            if (tempList == null) {
                tempList = new ArrayList();
                tempList.add(nsi);
                groupMap.put(nsi.getPjmc(), tempList);
            }
            else {
                tempList.add(nsi);
            }
        }
        Map sum=new HashMap();
        Iterator it = groupMap.keySet().iterator();
        while (it.hasNext()) {
            String pj = (String) it.next();
            List ns=(List)groupMap.get(pj);
            int sl=0;
            for(int i=0;i<ns.size();i++){
                NoteStorageInfo n=(NoteStorageInfo) ns.get(i);
                sl+=n.getSl();
            }
            sum.put(pj, new Integer(sl));
        }
        
        return sum;
    }

over

猜你喜欢

转载自www.cnblogs.com/wutongshu-master/p/12071832.html