map合并,相同键对应的值相加
最近在做统计钱的计算时遇到的一个需求,需要将一个大类别下的每一种钱进行特定的运算然后获得六年的钱,最后将这些钱按照年份进行汇总,获得总得大类型的六年的钱,在这个过程中采用了这种方法,每次算得钱放在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));
}
}
}
map合并,相同键对应的值相加的更多相关文章
- scala 两个map合并,key相同时value相加/相减都可
scala 两个map合并,key相同时value相加 1.map自带的合并操作 2.map函数 2.1示例 2.2合并两个map 3.用foldLeft 3.1 语法 3.2 合并两个map 1.m ...
- Python两个字典键同值相加的几种方法
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Jerry_1126/article/de ...
- (转载)php 合并数组中的数据,如果键值相等其值相加
(转载)http://hi.baidu.com/syxrrrr/item/7dfb2387343ce0874414cfdd /* * 合并数据,如果键值相等其值相加 * @param array $d ...
- LeetCode 677. Map Sum Pairs 键值映射(C++/Java)
题目: Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a ...
- js数组去重,id相同对某值相加合并
js数组去重,id相同对某值相加合并 案例1: 假设需要处理的数组结构. let arr =[ {id:'1', value:10}, {id:'1', value:20}, {id:'2', val ...
- PHP合并2个数字键数组的值
先要了解一个基础知识点:PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧 <?php /** * PHP合并2个数字键数组的值 * * @param arr ...
- Map:containsKey、containsValue 获取Map集合的键值的 值
get(Object key) 返回与指定键关联的值: containsKey(Object key) 如果Map包含指定键的隐射,则返回true: containsValue(Object valu ...
- Java: Map里面的键和值可以为空吗?
在Java中,Map里面的键和值可以为空吗?我们先来看一个例子: private static void TestHashMap() { // TODO Auto-generated method s ...
- 【Java必修课】通过Value获取Map中的键值Key的四种方法
1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...
随机推荐
- Java之集合(五)LinkedList
转载请注明源出处:http://www.cnblogs.com/lighten/p/7298017.html 1.前言 Java中另一个常见的list就是本章将要讲的LinkedList.ArrayL ...
- KMP-字符串模式匹配(c++/python实现)
KMP算法可以在O(n+m)的时间数量级上完成模式匹配,其做法在于:没当一次匹配过程中出现字符比较不等时,不需要回溯指针,而是利用已经得到的“部分匹配”的结果将模式向右“滑动”尽可能远的一段距离后,继 ...
- thinkphp 实现rabbitMq常驻进程消费队列
1,项目一级目录新建一个server文件 #!/usr/bin/env php <?php try { require __DIR__ . "/start.php"; } c ...
- Virtualbox下载与安装步骤
不多说,直接上干货! 本主主要介绍一下如何从官方网站下载正版的 虚拟化 Oracle VM VirtualBox ,以及说明一下去官方下载正版软件的重要性. 一.为了系统的稳定以及数据的安全,建议下载 ...
- Check类之duplicate declaration checking/Class name generation/Type Checking
1.duplicate declaration checking /** Check that variable does not hide variable with same name in * ...
- InterView之PHP
PHP HTTP Keep-Alive的作用 作用 Keep-Alive:使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.Web服务器 ...
- Servlet多文件上传方法
1. 通过getInputStream()取得上传文件. 001 /** 002 * To change this template, choose Tools | Templates 003 * ...
- Servlet多文件上传
各位大侠可能会对263电子邮箱中的"上传附件"功能有印象,就是:在浏览 器中点击"浏览",弹出一个对话框,选中文件后,单击"确定",文件就被 ...
- Beta--冲刺阶段合集
冲刺前计划与安排:https://www.cnblogs.com/pubg722/p/9069234.html 第一篇冲刺博客:http://www.cnblogs.com/pubg722/p/909 ...
- 1000. A-B
1000. A-B -----> http://soj.me/1000 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Descripti ...