最近在做统计钱的计算时遇到的一个需求,需要将一个大类别下的每一种钱进行特定的运算然后获得六年的钱,最后将这些钱按照年份进行汇总,获得总得大类型的六年的钱,在这个过程中采用了这种方法,每次算得钱放在map中,然后将map进行合并,写篇随笔mark下。

public class CombineMap {
public static Map<Integer,Integer> addTo(Map<Integer,Integer> target, HashMap<Integer,Integer>plus) {
Object[] os = plus.keySet().toArray();
Integer key;
for (int i=0; i<os.length; i++) {
key = (Integer)os[i];
if (target.containsKey(key))
target.put(key, target.get(key) + plus.get(key));
else
target.put(key, plus.get(key));
}
return target;
}
public static void main(String[] args) {
// 获得arm,body,head,leg,wst
// ......
Map<Integer,BigDecimal> all = new TreeMap<Integer,BigDecimal>();
all.put(2016, new BigDecimal(10));
all.put(2017, new BigDecimal(10));
all.put(2018, new BigDecimal(10));
all.put(2019, new BigDecimal(10));
all.put(2020, new BigDecimal(10));
      Map<Integer,BigDecimal> plus = new HashMap<Integer,BigDecimal>();
plus.put(2016, new BigDecimal(1));
plus.put(2017, new BigDecimal(2));
plus.put(2018, new BigDecimal(3));
plus.put(2019, new BigDecimal(4));
plus.put(2020, new BigDecimal(5));
      all = addTo(all, plus);
all = addTo(all, plus);
List<BigDecimal> tepList;
tepList = new ArrayList<BigDecimal>();
for (Object s : all.keySet()) {
System.out.print("year : " + s);
System.out.println(" values : " + all.get(s).doubleValue());
tepList.add(all.get(s));
}
}
}

最新文章

  1. 【Swift学习】Swift编程之旅---控制流(九)
  2. MySQL创建索引语法
  3. IOCP模型
  4. 在 slua 中使用更新的面向对象方案
  5. smtp协议
  6. KEIL C51 Call Tree
  7. HTML5 canvas绘制线条曲线
  8. SRM 219 Div II Level One: WaiterTipping,小心约分
  9. LayoutInflater (转)
  10. Android Studio开发快速创建MVP框架插件AndroidMVP
  11. 《Linux内核设计与实现》第十八章读书笔记
  12. 利用反射创建User类的对象
  13. HDU 1527 取石子游戏 (威佐夫博弈)
  14. leetcode987
  15. MEAN 26
  16. BZOJ3601 一个人的数论
  17. [Jsoi2016]最佳团体 BZOJ4753 01分数规划+树形背包/dfs序
  18. Android Things专题 1.前世今生
  19. Python入门一:简单得不能再简单了##
  20. haproxy-1.6.11 make 报错

热门文章

  1. 数据输入——生成你需要的echart图(世界地图,气泡图)
  2. PMP备考指南之第二章:项目运作环境
  3. 带你了解数据库中事务的ACID特性
  4. xgboost与gbdt区别
  5. InnoSetup打包时出现Interal error: CallSpawnServer: Unexpected response: $0.错误的解决办法
  6. The &quot;tsconfig.json&quot; file must have compilerOptions.sourceMap set to true
  7. automapper 自动映射 集成asp.net Core2.1
  8. 【拓扑 &amp;&amp; 模板】Kosaraju算法
  9. Java多线程学习之synchronized总结
  10. SpringMVC 面试题