分类:
2008-09-09 13:29:20
当hashMap的key是多个字段,且想得到多个value时,可能将key和 value分别做成List
view plaincopy to clipboardprint?
import java.util.Map;
//inquiry result
List fromDetailList = instHdDao.retrieveInstInfoByView(sysOwnerCd, fromCd, instruct);
// Initial Value
Integer dtTotalTruck = 0;
Integer dtTotalQuantity = 0;
Map hm = new HashMap();
Iterator it = fromDetailList.iterator();
while(it.hasNext()){
MeInstHd meInstHd = (MeInstHd)it.next();
List arr1 = new ArrayList();
List arr2 = new ArrayList();
List arr3 = new ArrayList();
String carrierCd = meInstHd.getCarrCd().trim();
arr1.add(carrierCd); // put keyValue to Arr1
if(hm.containsKey(arr1)){
// carrierCd is exist in hm
arr3 = (ArrayList)hm.get(arr1); // 通过Key arr1取得arr1的Value arr2.List arr3中有2列值,他们分别是dtTotalQuantity 和dtTotalTruck
dtTotalQuantity = (Integer)arr3.get(0) ;
dtTotalQuantity += meInstHd.sumInstructQty();
dtTotalTruck = (Integer)arr3.get(1) ;
dtTotalTruck += 1;
arr2.add(dtTotalQuantity);
arr2.add(dtTotalTruck);
hm.put(arr1, arr2); // creat hashMap hm.
}
else{
// carrierCd is not exist in hm
arr2.add(meInstHd.sumInstructQty()); //put value to arr2
arr2.add(1); // dtTotalTruck initial value is "1"
hm.put(arr1,arr2); // 其中arr1是key,arr2是value,可以通过get(arr1)来取得其value
}
}