n个List<Map>合并,Map中某属性值相等的value值相加
List<Map> maps1 =[{"bigtypes":100,"num":400},{"bigtypes":200,"num":300},{"bigtypes":400,"num":500},{"bigtypes":600,"num":500}]
List<Map> maps2 =[{"bigtypes":400,"num":720},{"bigtypes":500,"num":320},{"bigtypes":200,"num":120}]
List<Map> maps3 =[{"bigtypes":700,"num":120},{"bigtypes":500,"num":320},{"bigtypes":100,"num":510}]
目的为将 bigtypes 属性值相等 num属性值进行相加,bigtypes属性值不相等的List<Map>累加
将json数组循环赋值给 foodIntakeType
List<TDiet> tDiets = dietApiService.getDietsByDate(userId, startDate, endDate);
List<Map<String, String>> list = new ArrayList();
List<Map<String, String>> newList = new ArrayList();
int talls=0;
if(tDiets.size()>0){
int a=0;
for(TDiet td:tDiets){
talls+=Integer.parseInt(td.getFoodIntakeMeasure());//摄入总量
a++;
String foodIntakeType =td.getFoodIntakeType();
org.json.JSONArray jsonArray = new org.json.JSONArray(foodIntakeType);
int iSize = jsonArray.length();
for (int i = 0; i < iSize; i++) {
org.json.JSONObject jsonObj = jsonArray.getJSONObject(i);
String bigtypes = jsonObj.get("bigtypes").toString();
String num = jsonObj.get("num").toString();
Map<String, String> maMap = new HashMap<String, String>();
maMap.put("bigtypes", bigtypes);
maMap.put("num", num);
if(a>1){//多组数据
newList=list;
Map<String,String> newMap =new HashMap<String, String>();
int b=0;
for (int j = 0; j < list.size(); j++) {
newMap= list.get(j);
if(bigtypes.equals(newMap.get("bigtypes").toString())){//key值相等的:value相加
b++;
newList.remove(j);
Map<String, String> maMap2 = new HashMap<String, String>();
maMap2.put("bigtypes", bigtypes);
int xx= Integer.parseInt(newMap.get("num"))+Integer.parseInt(num);
maMap2.put("num",xx+"" );
newList.add(maMap2);
break;
}
}
if(b==0){//key值与list<map>的map无共同key的:list新增
newList.add(maMap);
}
list= newList;
}else{//只有一组数据
list.add(maMap);
}
} }
//取百分比
newList=new ArrayList();
for (int c = 0; c < list.size(); c++) {
Map<String,String> newMap2 =new HashMap<String, String>();
Map<String,String> oldmap=new HashMap<String, String>();
oldmap = list.get(c);
newMap2.put("bigtypes", oldmap.get("bigtypes"));
int aa= Integer.parseInt(oldmap.get("num"));
newMap2.put("num",towNumDivide(aa,talls)+"%");
newList.add(newMap2);
}
list=newList;
}
n个List<Map>合并,Map中某属性值相等的value值相加的更多相关文章
- 使用mybatis如果类属性名和数据库中的属性名不一样取值就会为null
使用mybatis时如果类属性名和数据库中的属性名不一样取值就会为null 这是不能再去改变javabean中的属性,只能改变sql语句.语句如下所示: <select id="sel ...
- CSS中 Zoom属性
CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...
- CSS中Position属性static、absolute、fixed、relative
在html中网页可以看成一个立体的空间,一个完整的页面是由很多个页面堆积形成的,如下图所示 CSS中Position属性有四个可选值,它们分别是:static.absolute.fixed.rel ...
- Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...
- 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 ...
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- EL表达式获取Map和List中的值
EL表达式获取Map和List中的值 EL表达式取Map中的值: 当Map中是String,String时 后台servlet中: Map<String, String> map1 = ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
- 如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。
原文:如何在WindowsPhone Bing Map控件中显示必应中国中文地图.谷歌中国中文地图. 最近正好有点业余时间,所以在做做各种地图.Bing Map控件本身就能显示必应地图,但是很遗憾微软 ...
随机推荐
- hdu_1848_Fibonacci again and again(博弈sg函数)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:给你3堆石子,每次只能取fibonacci数的石子,问先手是否能赢 题解:SG函数模版题 ...
- hdu_2446_Shell Pyramid(数学,二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2446 题意:题面很大,有用的就那么几句,意思就是用自然数来堆它画的那个金字塔,比如第一个金字塔的第一个 ...
- csuoj1009
AC代码: #include <iostream>#include <iomanip>using namespace std;//计算数学期望值,可以自己直接通过数组的方式来实 ...
- android网络编程之HttpUrlConnection的讲解--实现文件的断点上传
1.网络开发不要忘记在配置文件中添加访问网络的权限 <uses-permission android:name="android.permission.INTERNET"/& ...
- 当list做gridview的数据源时,可以用泛型来对list进行排序
当list做gridview的数据源时,可以用泛型来对list进行排序 ,代码如下 var temps = from t in list orderby t.paymentAmount descend ...
- properties读取的几种方法
第一种: private static Properties prop = new Properties(); static{ try { prop.l ...
- Redis配置文件 翻译 V3.2版本
# Redis配置文件例子. # # 注意:为了能读取到配置文件,Redis服务必须以配置文件的路径作为第一个参数启动 # ./redis-server /path/to/redis.conf # 关 ...
- find tar 压缩第一层目录,用于资料备份。
find *.tar.gz -exec tar zxvf '{}' \;//查找当前目录下的.tar.gz 的文件的 并发送给后面的命令执行find . -maxdepth 1 //查找当前目录下的文 ...
- Java学习笔记之接口和抽象类
接口(interface)1.interface创建一个接口,implements实现接口 interface jiekou{} class lie implements jiekou{}2.接口可以 ...
- 1.2 eclipse使用 :working set
working set可以是相当于文件夹~~有多个project时, 分别存放在不同的 workingset下,可以方便管理 新建或编辑 working set时,需要记住选择project *可参照 ...