java8Stream多字段分组并对多个字段求和
@Test
public void test1() {
//初始化集合数据
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> pareamMap = new HashMap<>();
pareamMap.put("name", "张三");
pareamMap.put("sex", "⼥");
pareamMap.put("age",20);
pareamMap.put("total", 1);
Map<String, Object> pareamMap1 = new HashMap<>();
pareamMap1.put("name", "李四");
pareamMap1.put("sex", "男");
pareamMap1.put("age",21);
pareamMap1.put("total", 1);
java streamMap<String, Object> pareamMap2 = new HashMap<>();
pareamMap2.put("name", "张三");
pareamMap2.put("sex", "⼥");
pareamMap2.put("age",20);
pareamMap2.put("total", 2);
Map<String, Object> pareamMap3 = new HashMap<>();
pareamMap3.put("name", "李四");
pareamMap3.put("sex", "男");
pareamMap3.put("age",21);
pareamMap3.put("total", 3);
list.add(pareamMap);
list.add(pareamMap1);
list.add(pareamMap2);
list.add(pareamMap3);
//根据姓名、性别、年龄分组
Map<String, List<Map<String, Object>>> resultMap =list.stream().upingBy(this::strList));;
List<Map<String, Object>> result = new ArrayList<>();
Map<String, Object> dataMap = new HashMap<>();
String arr [] =Key().split("_");
List<Map<String,Object>> list_one = Value();
dataMap.put("name",arr[0]);
dataMap.put("sex",arr[1]);
dataMap.put("age",arr[2]);
//求和
int total= list_one.stream().collect(Collectors.summingInt(o->Integer.parseInt(String.("total")))));
dataMap.put("total",total);
result.add(dataMap);
} );
System.out.println(result);
}
//拼接姓名+性别+年龄
private String strList(Map<String,Object> map){
("name")+"_"+("sex")+"_"+("age");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论