1.创建测试集合
List<Map<String,Object>> listMap = new ArrayList<>();
Map<String,Object> temp = new HashMap<>();
temp.put("name","张三");
temp.put("age",20);
temp.put("height",new BigDecimal("170.32"));
listMap.add(temp);
temp = new HashMap<>();
temp.put("name","李四");
temp.put("age",20);
temp.put("height",new BigDecimal("185.32"));
listMap.add(temp);
temp = new HashMap<>();
temp.put("name","王五");
temp.put("age",21);
temp.put("height",new BigDecimal("192.32"));
listMap.add(temp);
2.测试
2.1 BigDecimal 求和
BigDecimal sum = listMap.stream().map(e -> (BigDecimal)e.get("height")).reduce(BigDecimal.ZERO, BigDecimal::add);
2.2 BigDecimal 最大值
Map<String, Object> mapMax = listMap.stream().max((v1, v2) -> ((BigDecimal) v1.get("height")).compareTo((BigDecimal) v2.get("height"))).orElse(null);
2.3 BigDecimal 最小值
Map<String, Object> mapMin = listMap.stream().min((v1, v2) -> ((BigDecimal) v1.get("height")).compareTo((BigDecimal) v2.get("height"))).orElse(null);
2.4 List<Map<String,Object>> 分组
Map<String, List<Map<String, Object>>> mapGroup = listMap.stream().collect(Collectors.groupingBy(e -> e.get("name").toString()));
// 从数据库中出来的排列数据分组后乱序可以使用这个方法
Map<String, List<Map<String, Object>>> key = listMap.stream().collect(Collectors.groupingBy(e -> e.get("name").toString(), LinkedHashMap::new, Collectors.toList()));
2.5 List<Map<String,Object>> 排序
List<Map<String,Object>> mapSort = listMap.stream().sorted(Comparator.comparing(map->(BigDecimal) map.get("height"))).collect(Collectors.toList()); // 正序
Collections.reverse(mapSort); // 倒序
2.6 List<Map<String,Object>> 筛选
// 筛选 全部
List<Map<String,Object>> mapFindList = listMap.stream().filter(e -> "20".equals(e.get("age").toString())).collect(Collectors.toList());
// 查找一个
Map<String,Object> mapFindOne = listMap.stream().filter(p -> "20".equals(p.get("age").toString())).findAny().orElse(null);
2.7 List<Map<String,Object>> 抽取某一个字段
List<String> ListName = listMap.stream().map(e -> e.get("name").toString()).collect(Collectors.toList());
2.8 其他操作
// 求和
int ageAll = listMap.stream().mapToInt(e -> (int) e.get("age")).sum();
// 最大值
int ageMax = listMap.stream().mapToInt(e -> (int) e.get("age")).summaryStatistics().getMax();
// 最小值
int ageMin = listMap.stream().mapToInt(e -> (int) e.get("age")).summaryStatistics().getMin();
// 均值
double ageAvg = listMap.stream().mapToInt(e -> (int) e.get("age")).summaryStatistics().getAverage();
// 获取总数
long age = listMap.stream().mapToInt(e -> (int) e.get("age")).summaryStatistics().getCount();