JDK1.8新特性应用 ——List<Map<String,Object>>
使用stream对指定List<Map<String,Object>>进行排序
//map的value值转化为List<Map<String, Object>>形式
List<Map<String, Object>> result = new ArrayList<Map<String,Object>>(forcaseResulMap.values());
//对list根据里面的map结构的key为time的字段进行排序
result=result.stream().sorted((map1,map2)->{
return map1.get("time").toString().compareTo(map2.get("time").toString());
}).collect(Collectors.toList());
根据map中的score进行过滤
List<Map<String,Object>> list = Lists.newArrayList();
IntStream.range(1,5).forEach(e->{
Map<String,Object> map = Maps.newHashMap();
map.put("name","张三"+(e<3?e:e-1));
map.put("score", (int)(Math.random()*100)+1);
list.add(map);
});
System.out.println(list);
//过滤分数大于60的元素
List<Map<String, Object>> filterList = list.stream().filter(
e -> (int) e.get("score") > 60).collect(Collectors.toList());
System.out.println(filterList);
对集合中的map的value值求和
List<Map<String,Object>> list = Lists.newArrayList();
IntStream.range(1,5).forEach(e->{
Map<String,Object> map = Maps.newHashMap();
map.put("name","张三"+(e<3?e:e-1));
map.put("score", (int)(Math.random()*100)+1);
list.add(map);
});
System.out.println(list);
//通过Collect方式
int score = list.stream().collect(Collectors.summingInt(
e -> Integer.parseInt(e.get("score").toString())));
//通过map方式
int sum = list.stream().mapToInt(e -> Integer.parseInt(e.get("score").toString())).sum();
System.out.println(sum);
根据map的name进行分组,并将name相同的score值求和
List<Map<String,Object>> list = Lists.newArrayList();
IntStream.range(1,5).forEach(e->{
Map<String,Object> map = Maps.newHashMap();
map.put("name","张三"+(e<3?e:e-1));
map.put("score", (int)(Math.random()*100)+1);
list.add(map);
});
System.out.println(list);
List<Map<String, Object>> collectList = list.stream().collect(
Collectors.groupingBy(e -> e.get("name"))).values().stream().map(e -> {
Map<String, Object> map = e.get(0);
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(collectList);
Java8 stream 对List<Map<String,Object>> 去重
//构建数据
List<Map<String,Object>> list1 = new ArrayList<>();
Map<String,Object> map = new HashMap();
Map<String,Object> map2 = new HashMap();
map.put("key","1");
map.put("value","a");
map2.put("key","2");
map2.put("value","a");
list1.add(map2);
list1.add(map);
list1.forEach(System.out::println); //打印数据
List<Map<String, Object>> list2 = list1.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() ->new TreeSet<>(Comparator.comparing(m->m.get("value").toString()))
),ArrayList::new
)
); //根据value字段值去重,保留第一次放入list的map值
System.out.println("--------");
list2.forEach(System.out::println);
//结果打印:
//{value=a, key=2}
//{value=a, key=1}
//--------
//{value=a, key=2}
Java8 stream 更改List<Map<String,Object>> 里面Map对象的值
//构建对象
List<Map<String, Object>> list1 = new ArrayList<>();
Map<String, Object> map = new HashMap();
Map<String, Object> map2 = new HashMap();
map.put("key", "1");
map.put("value", "a");
map2.put("key", "2");
map2.put("value", "a");
list1.add(map2);
list1.add(map);
list1.forEach(System.out::println);
//将Map里面的每一个对象的key改为aaaa
List<Map<String, Object>> list2 = list1.stream().map(x -> {
x.put("key", "aaaa");
return x;
}).collect(Collectors.toList());
System.out.println("--------");
list2.forEach(System.out::println);
//{value=a, key=2}
//{value=a, key=1}
//--------
//{value=a, key=aaaa}
//{value=a, key=aaaa}
转载:https://www.cnblogs.com/zklymm/p/13986205.html