1.List<Map<String,String>>的遍历
思路就是使用双层for循环,外层先获得map里面所有键的set集合,内层就是循环set集合,我们知道了键,自然就能在map里面拿到值
代码演示
Map<String,String> map = new HashMap<>();
map.put("1","fds");
map.put("2","valu");
System.out.println("map" + map); //输出 map{1=fds, 2=valu}
//把map 添加进入list里面
java.util.List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(map);
System.out.println("list是什么样子" + list); //list是什么样子[{1=fds, 2=valu}]
//对list做遍历,拿到的还是map<String,String>
for(Map<String,String> m : list) {
//map.keySet() 方法返回值是:map包含的键的 set 视图(其实就是返回所有的键值,)放在set里面是保证set不重复
//循环所有的键值
for (String k :m.keySet()) {
//输出键以及对应的值
System.out.println(k + ":" +m.get(k));
}
}
输出
1:fds
2:valu
2.两个List<Map<String,String>>相互比较,有相同数据时某些字段相加减
代码实例
//inOrderList与outOrderList放自己的所比较的数据,name,number,id为map的key值
java.util.List<Map<String, String>> inOrderList = new ArrayList<Map<String, String>>();
java.util.List<Map<String, String>> outOrderList = new ArrayList<Map<String, String>>();
Map<String,String> map1 = new HashMap<>();
map1.put("number","33");
map1.put("name","ld");
map1.put("id","1");
inOrderList.add(map1);
Map<String,String> map2 = new HashMap<>();
map2.put("number","55");
map2.put("name","ld");
map2.put("id","1");
outOrderList.add(map2);
System.out.println("map2" + map2);
System.out.println("outOrderList是什么样子" + outOrderList);
/*map{number=55, name=ld, id=1}
list是什么样子[{number=55, name=ld, id=1}]*/
//也是使用双层循环,id 相同 name 相同就代表是同一条数据 ,number可以相加减
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+"");
}
}
}
System.out.println("加减之后inOrderList" + inOrderList);
}
输出结果
map2{number=55, name=ld, id=1}
outOrderList是什么样子[{number=55, name=ld, id=1}]
加减之后inOrderList[{number=-22, name=ld, id=1}]
3.单个List<Map<String,String>>相互比较,相同数据相加(减)
//3.单个List<Map<String,String>>相互比较,相同数据相加(减)
java.util.List<Map<String, String>> list = 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", "苏州市");
list.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", "泰州市");
list.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", "泰州市");
list.add(m3);
System.out.println("lsit" + list);
/*lsit'[
{date=2014-09-23 10:13:39, province=江苏省, city=苏州市, count=1, name=帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿, channel=东环大润发},
{date=2014-09-23 10:13:39, province=江苏省, city=泰州市, count=1, name=帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿, channel=东环大润发},
{date=2014-09-23 10:13:39, province=江苏省, city=泰州市, count=1, name=帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿, channel=东环大润发}]*/
java.util.List<Map<String, String>> countList = new ArrayList<Map<String,String>>();//用于存放最后的结果
for (int i = 0; i < list.size(); i++) {
//根据键获取对应的值
String count = list.get(i).get("count");
String name = list.get(i).get("name");
String province = list.get(i).get("province");
String date = list.get(i).get("date");
String channel = list.get(i).get("channel");
String city = list.get(i).get("city");
int flag = 0;//0为新增数据,就是数据不重复,在新初始化的列表添加一条新数据,1为增加count
//一般第一次循环列表插入一条记录,之后就是对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_ = list.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(list.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(list.get(i));
}
}
for (Map<String, String> map : countList) {
System.out.println(map);
}
}
结果输出
{date=2014-09-23 10:13:39, province=江苏省, city=苏州市, count=1, name=帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿, channel=东环大润发}
{date=2014-09-23 10:13:39, province=江苏省, city=泰州市, count=2, name=帮宝适纸尿裤L164 超薄干爽夏季透气专用婴儿尿不湿, channel=东环大润发}
4.对List<Map<String, Object>>类型数据进行简单处理
4.1构造初始list数据
java.util.List<Map<String, Object>> list = new ArrayList();
//IntStream.range(1,5) 左闭右开 会输出1 2 3 4
IntStream.range(1,5).forEach(e->{
Map<String,Object> map = new HashMap();
map.put("name","张三"+(e<3?e:e-1));
map.put("score", (int)(Math.random()*100)+1);
list.add(map);
});
System.out.println(list);
输出
[
{score=18, name=张三1},
{score=55, name=张三2},
{score=62, name=张三2},
{score=63, name=张三3}]
4.2对map中的score进行过滤操作
//过滤分数大于60
java.util.List<Map<String, Object>> filterList = list.stream().filter(e -> (int) e.get("score") > 60)
.collect(Collectors.toList());
System.out.println(filterList);
输出
[{score=62, name=张三2}, {score=63, name=张三3}]
4.3对map中的score进行排序操作
java.util.List<Map<String, Object>> sortList = list.stream().sorted((v1, v2) -> {
BigInteger b1 = new BigInteger(v1.get("score").toString());
BigInteger b2 = new BigInteger(v2.get("score").toString());
return b1.compareTo(b2);
}).collect(Collectors.toList());
System.out.println(sortList);
输出
[{score=18, name=张三1},
{score=55, name=张三2},
{score=62, name=张三2},
{score=63, name=张三3}]
4.4对map中的value进行求和操作
//求和
//方法1 通过Collect方式
Integer sum = list.stream().collect(Collectors.summingInt(
e -> Integer.parseInt(e.get("score").toString())
));
//方法2 通过map方式
/*int sum = list.stream().mapToInt(e -> Integer.parseInt(e.get("score").toString())).sum();*/
System.out.println(sum);
输出 : 198
4.5 根据map中的name进行分组????
//根据map的name进行分组,并将name相同的score值求和
java.util.List<Map<String, Object>> collect = list.stream().collect(
Collectors.groupingBy(e -> e.get("name"))).values().stream().map(e -> {
Map<String, Object> map = e.get(0);
//相同的score进行求和操作
map.put("score", e.stream().map(
s -> new BigInteger(s.get("score").toString())).reduce(BigInteger.ZERO, BigInteger::add));
return map;
}).collect(Collectors.toList());
System.out.println(collect);
输出
[{score=17, name=张三3}, {score=19, name=张三1}, {score=127, name=张三2}]