// 统计
public ArrayList summaryList(ArrayList arrayList, String Name) { ArrayList resultList = new ArrayList();
while (arrayList.size() > 1) {
HashMap hm1 = (HashMap) arrayList.get(0);
String totalAmount = "0";
String totalconvertAmount = "0";
for (int ii = 1; arrayList.size() > ii;) {
HashMap hm2 = (HashMap) arrayList.get(ii);
String key1 = (String) hm1.get(Name);
String key2 = (String) hm2.get(Name);
String cur1 = (String) hm1.get("Currency");
String cur2 = (String) hm2.get("Currency");
if (key1.hashCode() == key2.hashCode() && cur1.hashCode() == cur2.hashCode()) {
BigDecimal temp1 = new BigDecimal(0);
BigDecimal temp2 = new BigDecimal(0);
String sum1 = (String) hm1.get("Amount");
String sum2 = (String) hm2.get("Amount");
String sum3 = (String) hm1.get("ConvertRMB");
String sum4 = (String) hm2.get("ConvertRMB");
BigDecimal a = new BigDecimal(sum1);
BigDecimal b = new BigDecimal(sum2);
BigDecimal c = new BigDecimal(sum3);
BigDecimal d = new BigDecimal(sum4);
temp1 = temp1.add(a);
temp1 = temp1.add(b);
temp2 = temp2.add(c);
temp2 = temp2.add(d);
totalAmount = temp1.toString();
totalconvertAmount = temp2.toString();
hm1.put("Amount", totalAmount);
hm1.put("ConvertRMB", totalconvertAmount);
arrayList.remove(hm2);//与arrayList.remove(ii);的区别?
continue;
}
ii++;
}
resultList.add(hm1);
arrayList.remove(hm1);//与arrayList.remove(0);的区别? }
if (arrayList.size() == 1) {
resultList.add(arrayList.get(0));
}
return resultList;
}

解决方案 »

  1.   

    public static void main(String args[]){
    QueryTotalAsset a = new QueryTotalAsset();

    ArrayList list = new ArrayList();
    HashMap map1 = new HashMap();
    map1.put("Currency","01");
    map1.put("CDType","HQ");
    map1.put("Amount","0.0");
    map1.put("ConvertRMB","0.0");

    HashMap map2 = new HashMap();
    map2.put("Currency","01");
    map2.put("CDType","HQ");
    map2.put("Amount","2015.44");
    map2.put("ConvertRMB","2015.44");

    HashMap map3 = new HashMap();
    map3.put("Currency","01");
    map3.put("CDType","HQ");
    map3.put("Amount","67.69");
    map3.put("ConvertRMB","67.69"); list.add(map1);
    list.add(map2);
    list.add(map3);

    //区别
    // list.add(map2);   
    // list.add(map1);
    // list.add(map3);

    System.out.println(a.summaryList(list,"CDType"));

    }
      

  2.   

    执行结果:
    =====================================================================================
    remove(object):
    [{Amount=2083.13, ConvertRMB=2083.13, CDType=HQ, Currency=01}, {Amount=2015.44, ConvertRMB=2015.44, CDType=HQ, Currency=01}]
    ================================================
    remove(int):[{Amount=2083.13, ConvertRMB=2083.13, CDType=HQ, Currency=01}]
      

  3.   

    remove(object)是移除指定对象
    remove(int)是移除指定位置的对象
      

  4.   

    继续期待,请说明再具体点。为什么map1与map2对调后结果就有差异?
      

  5.   

    声明:不是我写的。
    我也奇怪,既然前面用get(int i)方式,后面就用remove(int i)呗,为什么换成remove(Object obj)
    如果用remove(int i)两次执行结果是一致的!!!我想坛子里明白的人帮解释下,是不是
      

  6.   

    出现这个结果的原因是map1的数据问题,如果后面不是两个0.0,其中任意一个换成0.1或者任何不是0的数字,结果应该就会正确。
    原因是他一直把相加总和存在第一个map里面,他原来设想是把总数相加之后,删除掉当前map,但是当第二个map出现的时候,总和相加了,但是数值确是hm1和hm2相同了,造成了这个list中有两个相同内容的map,然后remove(hm2),实际删除的却是hm1,以为这时hm1.equals(hm2)是true;
    后一次循环正常进行,并且结束。
    但是这时list里面剩下了一条原先map2的数据,而不是他想要的map1的数据,所以造成了remove失败。所以这种题目应该还是以remove(int i)的方式比较保险