前言: Java 8 已经发布很久了,大部分公司都还在使用java8 。很多报道表明Java 8 是一次重大的版本升级。在Java Code Geeks上已经有很多介绍Java 8新特性的文章,例如Playing with Java 8 – Lambdas and Concurrency、Java 8 Date Time API Tutorial : LocalDateTime和Abstract Class Versus Interface in the JDK 8 Era。本文综合了上述资料,整理了一些常用的语法特性,希望有所收获。
分组
Map<String, List<User>> listMap = list.stream().collect(Collectors.groupingBy(User::getSex));
for(String key:listMap.keySet()){
System.out.print(key+"组:");
listMap.get(key).forEach(user -> System.out.print(user.getName()));
System.out.println();
}
排序
list.stream().sorted(Comparator.comparing(user-> user.getAge()))
.forEach(user -> System.out.println(user.getName()));
过滤
list.stream().filter(user -> user.getSex().equals("男")).collect(Collectors.toList())
.forEach(user -> System.out.println(user.getName()));
多条件去重
list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(user -> user.getAge() + ";" + user.getName()))), ArrayList::new))
.forEach(user -> System.out.println(user.getName()));
最小值/最大值/平均值/求和
Integer min = list.stream().mapToInt(User::getAge).min().getAsInt();
Integer max = list.stream().mapToInt(User::getAge).max().getAsInt();
Double average = list.stream().mapToInt(User::getAge).average().getAsDouble();
Integer sum = list.stream().mapToInt(User::getAge).sum();
System.out.println("最小值:"+min+", 最大值"+max+", 平均值:"+average+", 和:"+sum);
分组/求和/计算
//求和
Map<String, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(User::getSex, Collectors.summarizingInt(User::getAge))
//分组计数
Map<String, Long> colorNum = appleList.stream().collect(Collectors.groupingBy(Apple::getColor, Collectors.counting()));
//分组1
Map<String, List<Map.Entry<String, Object>>> paramOut = mapParam.entrySet().stream().filter(map -> map.getKey().contains("paramOut")).collect(Collectors.groupingBy(map -> {
String key = map.getKey();
return key.substring(0, key.indexOf("-", 9));
}));
//分组2
Map<String,List<Map<String,Object>>> mapList = list.stream().
collect(Collectors.groupingBy((Map m) -> (String) m.get("分组")));
//分组3
Map<Long, List<Long>> collect = memberOrgRecordDtos.stream().
collect(Collectors.groupingBy(MemberOrgRecordDto::getRoomId, Collectors.mapping(FoodOrderServiceImpl::fetchMemberIdValue, Collectors.toList())));
//求和
Map<String, Integer> collect = maps.stream().collect(Collectors.groupingBy(x -> String.valueOf(x.get("name")), Collectors.summingInt(x -> Integer.parseInt(String.valueOf(x.get("value"))))));
listToMap
userlist.stream().collect(Collectors.toMap(User::getAge, Function.identity(),(key1,key2)->key2));
List<String>转List<Integer>
List<Integer> intList = strList.stream().map(Integer::parseInt).collect(Collectors.toList());
根据key合并数据
public static List<Map<String, Object>> merge(List<TreeMap<String, Object>> m1,String mergeKey){
Set<String> set = new HashSet<>();
System.out.println("m1的数据格式是:"+m1);
return m1.stream()
.filter(map->map.get(mergeKey)!=null)
.collect(Collectors.groupingBy(o->{
//暂存所有key
set.addAll(o.keySet());
//按mergeKey分组
return o.get(mergeKey).toString();
}))
.entrySet().stream().map(o->{
//合并
Map<String, Object> map = o.getValue().stream().flatMap(m->{
return m.entrySet().stream();
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a,b)->b));
//为没有的key赋值0
set.stream().forEach(k->{
if(!map.containsKey(k)) map.put(k, 0);
});
return map;
}).collect(Collectors.toList());
}
交集/并集/差集/去重
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);