List<Map<String,String>>操作(遍历,比较)
1.List<Map<String,String>>的遍历:
Map<String,String> map = new HashMap<String, String>(); map.put("1", "fds");
map.put("2", "valu");
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(map);
for (Map<String, String> m : list){
for (String k : m.keySet()){
System.out.println(k + " : " + m.get(k));
} }
2.两个List<Map<String,String>>相互比较,有相同数据时某些字段相加减
inOrderList与outOrderList放自己的所比较的数据,name,number,id为map的key值
for (int i = 0; i < inOrderList.size(); i++) {//循环获取入库数据
String number1 = String.valueOf(inOrderList.get(i).get("number"));
String depotName1 = inOrderList.get(i).get("name");
String proId1 = inOrderList.get(i).get("id");
for (int j = 0; j < outOrderList.size(); j++) {//循环获取出库数据
String number2 = String.valueOf(outOrderList.get(j).get("number"));
String depotName2 = outOrderList.get(j).get("name");
String proId2 = outOrderList.get(j).get("id");
if (depotName1.equals(depotName2) && proId1.equals(proId2)) {
Integer sum =Integer.parseInt(number1) - Integer.parseInt(number2);//判断是否为同一条数据,同条数据数量相减
inOrderList.get(i).put("number", sum + "");
}
}
}
3.单个List<Map<String,String>>相互比较,相同数据相加(转)
List<Map<String, String>> l = new ArrayList<Map<String,String>>();
Map<String, String> m1 = new HashMap<String, String>(); m1.put("count", "1");
m1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
m1.put("province", "江苏省");
m1.put("date", "2014-09-23 10:13:39");
m1.put("channel", "东环大润发");
m1.put("city", "苏州市");
l.add(m1); Map<String, String> m2 = new HashMap<String, String>();
m2.put("count", "1");
m2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
m2.put("province", "江苏省");
m2.put("date", "2014-09-23 10:13:39");
m2.put("channel", "东环大润发");
m2.put("city", "泰州市");
l.add(m2); Map<String, String> m3 = new HashMap<String, String>();
m3.put("count", "1");
m3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
m3.put("province", "江苏省");
m3.put("date", "2014-09-23 10:13:39");
m3.put("channel", "东环大润发");
m3.put("city", "泰州市");
l.add(m3); List<Map<String, String>> countList = new ArrayList<Map<String,String>>();//用于存放最后的结果
for (int i = 0; i < l.size(); i++) {
String count = l.get(i).get("count");
String name = l.get(i).get("name");
String province = l.get(i).get("province");
String date = l.get(i).get("date");
String channel = l.get(i).get("channel");
String city = l.get(i).get("city"); int flag = 0;//0为新增数据,1为增加count
for (int j = 0; j < countList.size(); j++) {
String count_ = countList.get(j).get("count");
String name_ = countList.get(j).get("name");
String province_ = countList.get(j).get("province");
String date_ = countList.get(j).get("date");
String channel_ = l.get(j).get("channel");
String city_ = countList.get(j).get("city"); if (count.equals(count_)&&
name.equals(name_)&&
province.equals(province_)&&
date.equals(date_)&&
channel.equals(channel_)&&
city.equals(city_)) {
int sum = Integer.parseInt(l.get(i).get("count")) + Integer.parseInt(countList.get(j).get("count"));
countList.get(j).put("count", sum + "");
flag = 1;
continue;
}
}
if (flag == 0) {
countList.add(l.get(i));
}
}
for (Map<String, String> map : countList) {
System.out.println(map);
}
}
public class Test { /**
* @param args
*/
public static void main(String[] args) {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map1 = new HashMap<String,String>();
map1.put("count", "1");
map1.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map1.put("province", "江苏省");
map1.put("date", "2014-09-23 10:13:39");
map1.put("channel", "东环大润发");
map1.put("city", "苏州市");
list.add(map1); Map<String,String> map2 = new HashMap<String,String>();
map2.put("count", "1");
map2.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map2.put("province", "江苏省");
map2.put("date", "2014-09-23 10:13:39");
map2.put("channel", "东环大润发");
map2.put("city", "泰州市");
list.add(map2); Map<String,String> map3 = new HashMap<String,String>();
map3.put("count", "1");
map3.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map3.put("province", "江苏省");
map3.put("date", "2014-09-23 10:13:39");
map3.put("channel", "东环大润发");
map3.put("city", "泰州市");
list.add(map3); Map<String,String> map4 = new HashMap<String,String>();
map4.put("count", "1");
map4.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map4.put("province", "江苏省");
map4.put("date", "2014-09-23 10:13:39");
map4.put("channel", "东环大润发");
map4.put("city", "苏州市");
list.add(map4); System.out.println(mergeList(list)); } /**
* 合并相同的Map
* @param list
* @return
*/
public static List<Map<String,String>> mergeList(List<Map<String,String>> list){
List<Map<String,String>> retList = new ArrayList<Map<String,String>>();
for(int i=0;i<list.size();i++){
Map<String,String> map = list.get(i);
String name = map.get("name");
String city = map.get("city");
String count = map.get("count");
Map<String,String> newMap=isExistSame(i,name,city,count,list);
if(null==newMap){
retList.add(map);
}
else{
list.remove(map.get(i));
retList.add(newMap);
}
}
return retList;
} private static Map<String,String> isExistSame(int i,String name,String city,String count,List<Map<String,String>> list){
Map<String,String> newMap = null;
for(int j=i+1;j<list.size();j++){
Map<String,String> innerMap = list.get(j);
String name1 = innerMap.get("name");
String city1 = innerMap.get("city");
String count1 = innerMap.get("count");
if(name.equals(name1)&&city.equals(city1)){
newMap = new HashMap<String,String>();
newMap.put("count", ""+(Integer.parseInt(count)+Integer.parseInt(count1)));
Iterator<String> keyIterator = innerMap.keySet().iterator();
while(keyIterator.hasNext()){
String key = keyIterator.next();
if(!key.equals("count")){
newMap.put(key, innerMap.get(key));
}
}
list.remove(j);
}
}
return newMap;
}
}
ublic static void main(String[] args) {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
boolean m = true;
for(int i=0;i<10000;i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("count", "1");
map.put("name", "帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿");
map.put("province", "江苏省");
map.put("date", "2014-09-23 10:13:39");
map.put("channel", "东环大润发");
map.put("city", m ? "苏州市":"泰州市");
m = !m;
list.add(map);
}
System.out.println(System.currentTimeMillis());
for(int i=list.size();--i>-1;){
Map<String, String> src = list.get(i);
int same = 0;
for(int j=i-1;j>-1;j--){
Map<String, String> tar = list.get(j);
if(equals(src, tar)){
int count = Integer.parseInt(src.get("count"))
+Integer.parseInt(tar.get("count"));
src.put("count", count+"");
list.remove(j);
same++;
}
}
i -=same;
}
System.out.println(list);
System.out.println(System.currentTimeMillis());
} public static boolean equals(Map<String, String> src,Map<String, String> tar){
Set<Entry<String,String>> entrySet = src.entrySet();
String srcCount = src.remove("count");//count不参与比较
String srcValue = null,tarValue = null;
for (Iterator<Entry<String,String>> it = entrySet.iterator(); it.hasNext();) {
Entry<String, String> entry = it.next();
srcValue = entry.getValue();
tarValue = tar.get(entry.getKey());
if(srcValue.length()!=tarValue.length()||!srcValue.equals(tarValue)){
src.put("count", srcCount);
return false;
}
}
src.put("count", srcCount);
return true;
}
List<Map<String,String>>操作(遍历,比较)的更多相关文章
- Map<String, String>循环遍历的方法
Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 下 ...
- 【Java】遍历Map<String,String>
Map<String, String> map = new HashMap<>(); map.put("key1", "value1") ...
- Map<String, String> 遍历的四种方法
Map<String, String> map = new HashMap<String, String>(); map.put("key1", " ...
- 入门:Java Map<String,String>遍历及修改
重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...
- List<Map<String, String>>和Map<String, List<String>>遍历
public void TestM() { List<Map<String, String>> lm = new ArrayList<>(); Ma ...
- JSON数组形式字符串转换为List<Map<String,String>>的几种方法
package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArr ...
- getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:
直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ...
- FastJSON 简介及其Map/JSON/String 互转
在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...
- JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类
<pre name="code" class="java"></pre><pre name="code" cl ...
随机推荐
- SVM对偶形式
dual svm 对偶SVM linear SVM 可以用二次规划方法解 xn通过非线性转换变成zn SVM配合非线性特征转换 透过large-margin降低模型复杂度 透过特征转换得到弯弯曲曲的边 ...
- prime,素数的判断——c语言
输入一个数a,求他是否是素数(用函数) 程序: #include<stdio.h> int prime(int a)-----------------------------------/ ...
- [CSS]border边框
border: 1px solid #ccc; /*1像素 实线 灰色*/可分割成:border-width:1px;border-style: solid; border-color: #00 ...
- android sdk api的层次结构
一.系统级:android.accounts android.app 1.OS 相关 android.os android.os.storage ...
- Administration Commands
Commands useful for administrators of a hadoop cluster. balancer Runs a cluster balancing utility. A ...
- adb找不到设备
提示信息如下所示: adb server is out of date.killing... adb server didn't ACK *failed to start daemon * error ...
- 学学Whatsapp,如何让自己挣160亿美金,然后退休?开发个J2ME应用。
facebook用160亿美元收购了Whatsapp,要知道这是facebook市值1600亿美元的十分之一,而Whatsapp是一个只有50名员工的小公司,这个价格让硅谷各种科技公司大佬跌破镜框.其 ...
- CENTOS elasticsearch plugin install:Failed: SSLException[java.security.ProviderException,解决
安装Elasticsearch插件时总报SSLException yum upgrade nss 解决
- ASP.NET MVC轻教程 Step By Step 9——分页
现在我们要把Index视图的留言信息进行分页显示. Step 1. 创建路由 我们希望以类似地址http://localhost:41583/Page1来表示第一页,Page2表示第二页,以此类推.在 ...
- mysql update from 子查询
mssql 子查询更新 update log set uin= b.uinfrom log a,logs bwhere a.accountuin = b.accountuin mysql 不支持 up ...