黑马程序员技术交流社区
标题:
使用集合嵌套计算银行卡消费总额
[打印本页]
作者:
vincentgood
时间:
2014-5-30 19:27
标题:
使用集合嵌套计算银行卡消费总额
package day25;
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。
*/
public class SumMoney {
// 初始化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("bankCode", "300201");
map0.put("money", "10980.10");
lists.add(map0);
Map<String, String> map1 = new HashMap<String, String>();
map1.put("bankCode", "300201");
map1.put("money", "20300.20");
lists.add(map1);
Map<String, String> map2 = new HashMap<String, String>();
map2.put("bankCode", "300202");
map2.put("money", "400.40");
lists.add(map2);
Map<String, String> map3 = new HashMap<String, String>();
map3.put("bankCode", "300202");
map3.put("money", "500.50");
lists.add(map3);
Map<String, String> map4 = new HashMap<String, String>();
map4.put("bankCode", "300203");
map4.put("money", "3203");
lists.add(map4);
Map<String, String> map5 = new HashMap<String, String>();
map5.put("bankCode", "300203");
map5.put("money", "3003.5");
lists.add(map5);
Map<String, String> map6 = new HashMap<String, String>();
map6.put("bankCode", "300209");
map6.put("money", "3003.5");
lists.add(map6);
return lists;
}
/**
* 统计每张银行卡金额
*/
public static List<Map<String, String>> sumMoney(
List<Map<String, String>> list, String bankCode, String money) {
// 返回的List
List<Map<String, String>> returnList = new ArrayList<Map<String, String>>();
// 接收每一行的数据--Map
Map<String, String> lineMap = 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
lineMap = (Map<String, String>) list.get(i);
Iterator<Entry<String, String>> itor = lineMap.entrySet()
.iterator();
while (itor.hasNext()) {
Entry<String, String> entry = itor.next();
if (entry.getKey().equals(bankCode)) {
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(bankCode)) {
if (tempEntry.getValue().equals(
entry.getValue())) {
// 返回的List的Map
Map<String, String> returnMap = new HashMap<String, String>();
returnMap.put(bankCode,
tempEntry.getValue());
Object object0 = tempMap.get(money);
Object object1 = lineMap.get(money);
BigDecimal bd0 = new BigDecimal(
object0.toString());
BigDecimal bd1 = new BigDecimal(
object1.toString());
String bd = bd0.add(bd1).toString();// 收集另一个字段整数相加
returnMap.put(money, bd);
returnList.remove(j);// 移除原来的Map
returnList.add(returnMap);// 添加新Map
count = 0;
} else {
++count;
}
}
}
}
if (count == returnList.size()) {
Map<String, String> returnMap = new HashMap<String, String>();
returnMap.put(bankCode, lineMap.get(bankCode)
.toString());
returnMap.put(money, lineMap.get(money).toString());
returnList.add(returnMap);
}
} else {
returnList.add(lineMap);
}
}
}
}
return returnList;
}
public static void main(String[] args) {
List<Map<String, String>> list = init();
List<Map<String, String>> lists = sumMoney(list,
"bankCode", "money");
for (int i = 0; i < lists.size(); i++) {
@SuppressWarnings({ "unchecked", "rawtypes" })
Map<String, String> map = (Map) lists.get(i);
System.out.println("银行卡ID:" + map.get("bankCode") + " "
+ "消费总金额:" + map.get("money"));
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2