我是靠谱客的博主 失眠月饼,最近开发中收集的这篇文章主要介绍5、java8的collector,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、collectorAPI

  1. averagingInt

  1. counting

  1. collectingAndThen

  1. groupingBy

  1. joining,把字符串链接

  1. MaxBy

  1. reducing

  1. toMap

  1. toList

2、案例

package java8;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.math3.fitting.leastsquares.EvaluationRmsChecker;
import org.apache.flink.calcite.shaded.com.google.common.collect.Maps;
import scala.App;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class CollectorExam6 {
public static void main(String[] args) {
Apple greenApple = new Apple("green", 150);
Apple redApple = new Apple("red", 105);
Apple greenApple1 = new Apple("green", 150);
Apple redApple1 = new Apple("red", 105);
List<Apple> appleList = Lists.newArrayList();
appleList.add(greenApple);
appleList.add(redApple);
appleList.add(greenApple1);
appleList.add(redApple1);
//
System.out.println(groupByNormal(appleList));
//
System.out.println(groupByFunctionalAndOptional(appleList));
//groupingBy
//
System.out.println(groupByCollector(appleList));
//averagingInt
Optional.ofNullable(appleList.stream().collect(Collectors.averagingInt(apple -> {
return apple.getWeight();
}))).ifPresent(System.out::println);
Optional.ofNullable(appleList.stream().collect(Collectors.averagingInt(Apple::getWeight)))
.ifPresent(System.out::println);
//counting
Optional.ofNullable(appleList.stream().collect(Collectors.counting())).ifPresent(System.out::println);
//collectingAndThen
Optional.ofNullable(appleList.stream().collect(Collectors.collectingAndThen(Collectors.counting(), count -> {
return count + "#";
}))).ifPresent(System.out::println);
//groupingBy
Optional.ofNullable(appleList.stream().collect(Collectors.groupingBy(Apple::getColor, Collectors.averagingInt(Apple::getWeight))))
.ifPresent(System.out::println);
//joining,把字符串链接
String joiningRes = appleList.stream().map(Apple::getColor).collect(Collectors.joining(","));
Optional.ofNullable(joiningRes).ifPresent(System.out::println);
//MaxBy
Optional<Apple> maxByRes = appleList.stream().collect(Collectors.maxBy(Comparator.comparing(Apple::getWeight)));
maxByRes.ifPresent(System.out::println);
//reducing
Integer weightSum = appleList.stream().map(Apple::getWeight).collect(Collectors.reducing(0, (a, b) -> {
return a + b;
}));
System.out.println(weightSum);
//toCollection
Optional.ofNullable(appleList.stream().collect(Collectors.toCollection(HashSet::new)))
.ifPresent(System.out::println);
//toMap,必须写merge方法,否则如果key相同则报错
Optional.ofNullable(appleList.stream().collect(Collectors.toMap(Apple::getColor, Apple::getWeight, (weight1, weight2) -> {
return weight1 + weight2;
})))
.ifPresent(System.out::println);
}
private static Map<String, List<Apple>> groupByNormal(List<Apple> appleList) {
Map<String, List<Apple>> listMap = Maps.newHashMap();
appleList.forEach(apple -> {
List<Apple> list = listMap.get(apple.getColor());
if (list == null) {
list = Lists.newArrayList();
listMap.put(apple.getColor(), list);
}
list.add(apple);
});
return listMap;
}
//不用判空了
private static Map<String, List<Apple>> groupByFunctionalAndOptional(List<Apple> appleList) {
Map<String, List<Apple>> listMap = Maps.newHashMap();
appleList.stream().forEach(apple -> {
List<Apple> appleList1 = Optional.ofNullable(listMap.get(apple.getColor())).orElseGet(ArrayList::new);
listMap.put(apple.getColor(), appleList1);
appleList1.add(apple);
});
return listMap;
}
//Collectors的group by
private static Map<String, List<Apple>> groupByCollector(List<Apple> appleList) {
Map<String, List<Apple>> collectRes = appleList.stream().collect(Collectors.groupingBy(Apple::getColor));
return collectRes;
}
}

最后

以上就是失眠月饼为你收集整理的5、java8的collector的全部内容,希望文章能够帮你解决5、java8的collector所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部