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);  //這里的tempList表示的是groupMap.get(nsi.getPjmc())的引用,所以往里面追加值也就會改變map里的值
            }
		}
		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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。