package com.mamcharge.integrate.core; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * 描述: List中相同的key的Map的另一个字段值之和 * 作用: List中存在多张银行卡清单流水,有银行卡id,消费金额, 统计一张银行卡总消费额。返回list。 * 版权: Copyright (c) 2017 * 作者: 蔡高情 * 版本: 1.0 * 创建日期: 2014-5-29 * 创建时间: 下午06:53:01 */ public class AddMapkeyListInTheSame { // 初始化List<Map<String, String>> public static List<Map<String, String>> init() { List<Map<String, String>> lists = new ArrayList<Map<String, String>>(); Map<String, String> map0 = new HashMap<String, String>(); map0.put("name", "济南"); map0.put("value", "50"); lists.add(map0); Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", "济南"); map1.put("value", "55"); lists.add(map1); Map<String, String> map2 = new HashMap<String, String>(); map2.put("name", "济宁"); map2.put("value", "40"); lists.add(map2); Map<String, String> map3 = new HashMap<String, String>(); map3.put("name", "济宁"); map3.put("value", "300"); lists.add(map3); Map<String, String> map4 = new HashMap<String, String>(); map4.put("name", "北京"); map4.put("value", "300203"); lists.add(map4); Map<String, String> map5 = new HashMap<String, String>(); map5.put("name", "北京"); map5.put("value", "300203.5"); lists.add(map5); Map<String, String> map6 = new HashMap<String, String>(); map6.put("name", "300209"); map6.put("value", "300203.5"); lists.add(map6); return lists; } /** * 统计每张银行卡金额 */ public static List<Map<String, String>> statisticsOfEachBankCardAmount(List list, String name, String value) { //返回的List List<Map<String, String>> returnList = new ArrayList(); //接收每一行的数据--Map Map<String, String> oneMap = new HashMap<String, String>(); //临时List的Map Map<String, String> tempMap = new HashMap<String, String>(); //for 取出每一行的Map for (int i = 0; i < list.size(); i++) { //每行数据转成Map oneMap = (Map<String, String>) list.get(i); Iterator<Entry<String, String>> itor = oneMap.entrySet().iterator(); while (itor.hasNext()) { Entry<String, String> entry = itor.next(); if (entry.getKey().equals(name)) { if (returnList.size() != 0) { int count = 0; for (int j = 0; j < returnList.size(); j++) { tempMap = (Map<String, String>) returnList.get(j); Iterator<Entry<String, String>> tempItor = tempMap.entrySet().iterator(); while (tempItor.hasNext()) { Entry<String, String> tempEntry = tempItor.next(); if (tempEntry.getKey().equals(name)) { if (tempEntry.getValue().equals(entry.getValue())) { //返回的List的Map Map<String, String> returnMap = new HashMap<String, String>(); returnMap.put(name, tempEntry.getValue()); Object object0 = tempMap.get(value); Object object1 = oneMap.get(value); BigDecimal bd0 = new BigDecimal(object0.toString()); BigDecimal bd1 = new BigDecimal(object1.toString()); //收集另一个字段整数相加 String bd = bd0.add(bd1).toString(); returnMap.put(value, bd); //移除原来的Map returnList.remove(j); //添加新Map returnList.add(returnMap); count = 0; } else { ++count; } } } } if (count == returnList.size()) { Map<String, String> returnMap = new HashMap<String, String>(); returnMap.put(name, oneMap.get(name).toString()); returnMap.put(value, oneMap.get(value).toString()); returnList.add(returnMap); } } else { returnList.add(oneMap); } } } } return returnList; } public static void main(String[] args) { List<Map<String, String>> list = init(); List lists = statisticsOfEachBankCardAmount(list, "name", "value"); for (int i = 0; i < lists.size(); i++) { Map map = (Map) lists.get(i); System.out.println("银行卡ID:" + map.get("name") + " " + "消费总金额:" + map.get("value")); } } }
统计一张银行卡总消费额JAVA
最新推荐文章于 2022-08-12 14:15:48 发布