2019-04-29——Java8 Collectors常用方法
2019-04-29 本文已影响0人
烟雨乱平生
@Data
@AllArgsConstructor
static class Achievement{
private String name;
private double score;
private String date;
}
static List<Achievement> getData(){
List<Achievement> list = new ArrayList<>();
list.add(new Achievement("wang",80,"2017-10-20"));
list.add(new Achievement("wang",90,"2017-5-20"));
list.add(new Achievement("li",90,"2018-10-20"));
list.add(new Achievement("zhao",50,"2017-10-20"));
list.add(new Achievement("qian",60,"2017-10-22"));
list.add(new Achievement("sun",20,"2017-10-23"));
list.add(new Achievement("sun",20,"2017-11-23"));
return list;
}
/*寻找最大值*/
private static void c1(){
Optional<Integer> collect = IntStream.rangeClosed(0, 100).boxed().collect(Collectors.maxBy(Comparator.comparingInt(a -> a)));
System.out.println(collect.get());
}
/*寻找最小值*/
private static void c2(){
Optional<Integer> collect = Stream.generate(Math::random).limit(20).map(a -> {
Double s = (a * 1000);
return s.intValue();
}).collect(Collectors.minBy(Comparator.comparingInt(a -> a)));
System.out.println(collect.get());
}
/*统计数量*/
private static void c3(){
Long collect = Stream.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10).collect(Collectors.counting());
System.out.println(collect);
}
/*汇总求和*/
private static void c4(){
Integer collect = Stream.iterate(0, n -> n + 2).limit(100).collect(Collectors.summingInt(a -> a));
System.out.println(collect);
}
/*连接字符串*/
private static void c5(){
String collect = Stream.of("上海", "快仓", "科技", "有限", "公司").collect(Collectors.joining());
System.out.println(collect);
}
/*单级分组*/
private static void c6(){
Map<String, List<Achievement>> collect = getData().stream().collect(Collectors.groupingBy(Achievement::getName));
System.out.println(collect);
}
/*多级分组*/
private static void c7(){
Map<String, Map<Double, List<Achievement>>> collect = getData().stream().collect(Collectors.groupingBy(Achievement::getName, Collectors.groupingBy(Achievement::getScore)));
System.out.println(collect);
}
/*按组统计*/
private static void c8(){
Map<String, Long> collect = getData().stream().collect(Collectors.groupingBy(Achievement::getName, Collectors.counting()));
System.out.println(collect);
}
/*按组收集*/
private static void c9(){
Map<String, Optional<Achievement>> collect = getData().stream().collect(Collectors.groupingBy(Achievement::getName, Collectors.maxBy(Comparator.comparingInt(a -> (int) a.getScore()))));
System.out.println(collect);
}
/*分区*/
private static void c10(){
Map<Boolean, List<Achievement>> collect = getData().stream().collect(Collectors.partitioningBy(a -> a.getScore() >= 60));
System.out.println(collect);
}
/*分区分组*/
private static void c11(){
Map<Boolean, Map<String, List<Achievement>>> collect = getData().stream().collect(Collectors.partitioningBy(a -> a.getScore() >= 60, Collectors.groupingBy(Achievement::getName)));
System.out.println(collect);
}
/*转换*/
private static void c12(){
List<String> collect = getData().stream().collect(Collectors.mapping(Achievement::getName, Collectors.toList()));
System.out.println(collect);
}
/*收集后处理*/
private static void c13(){
Achievement collect = getData().stream().collect(Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingDouble(Achievement::getScore)), Optional::get));
System.out.println(collect);
}