java8 ListMap集合的基本操作
前言
// 创建一个listmap集合
List<Map<String,Object>> obList = new ArrayList<>();
Map<String,Object> obMap = new HashMap<>();
obMap.put("dd",112);
obMap.put("rr",1);
obList.add(obMap);
obMap = new HashMap<>();
obMap.put("dd",11);
obMap.put("rr",1);
obList.add(obMap);
obMap = new HashMap<>();
obMap.put("dd",111);
obMap.put("rr",11);
obList.add(obMap);
- list集合中通过map某个值排序
obList = obList
.stream()
.sorted((i,j) -> Integer.parseInt(i.get("dd").toString()) - Integer.parseInt(j.get("dd").toString()) )
.collect(Collectors.toList());
System.out.println(obList);
- list集合中通过map中通过某个值分组
Map<Object, List<Map<String, Object>>> rr =
obList.stream()
.collect(Collectors.groupingBy(obj -> obj.get("rr")));
System.out.println(rr);
- list集合中通过map中通过某个值分组并去重
Map<Object, Map<String, Object>> rr1 =
obList.stream()
.collect(Collectors.toMap(ob -> ob.get("rr"), c -> c, (k1, k2) -> k1));
System.out.println(rr1);
- 通过list集合中的map的某个值过滤
List<Map<String, Object>> rr2 = obList.stream().filter(map -> (int) map.get("rr") != 1).collect(Collectors.toList());
System.out.println(rr2);
- list中获取对应数据
IntSummaryStatistics stats = obList.stream()
.mapToInt(ob -> (int) ob.get("rr")).summaryStatistics();
System.out.println("列表 : " + obList);
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());