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控件本身就能显示必应地图,但是很遗憾微软 ...
随机推荐
- Educational Codeforces Round 15_B. Powers of Two
B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Storm官方文档翻译之设置开发环境
本文将介绍如何设置Storm的开发环境.下面是大纲: 1.下载Storm发布包,解压,将解压的 bin目录放到你电脑的PATH中. 2.为了能够在远程集群中启动或者停止Topology,请将集群信息放 ...
- 用java的socket来发送一个类
用socket可以简单的发送一些文本信息,太复杂的可能发送不了,比如图片音频可能要用到http来发送和接收了.最基本的使用socket来发送一个字符串,但有的时候我们希望能够发送一个类,这样serve ...
- requirejs 一个拆分js项目的类库
http://www.requirejs.cn/ http://requirejs.org/docs/start.html
- 解读QML之一
http://cache.baiducontent.com/c?m=9d78d513d98002b8599dcb201a17a7374408c6347691c4523f8a9c12d522195646 ...
- cscope
http://sourceforge.net/p/cscope/bugs/247/ buffer read only? cx cq Here is a simple patch which re-en ...
- C#中使用正则表达式提取超链接地址的集中方法
一般在做爬虫或者CMS的时候经常需要提取 href链接或者是src地址.此时可以使用正则表达式轻松完成. Regex reg = new Regex(@"(?is)<a[^>]* ...
- C++设计模式-Visitor访问者模式
#include <iostream> #include <string> #include <string.h> #include <memory> ...
- 使用dom4j解析XML例子
包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), TestDom4jReadExmple.java(测试解析的结果) 代 ...
- JS day01——window对象、执行顺序、线程模型
1.window对象 window对象表示当前浏览器的窗口,它是一个顶级对象,我们创建的所有对象.函数.变量都是window对象的成员. window对象自带了一些非常有用的方法.属性. window ...