Java 8 – 收集器Collectors_实战
JAVA 实战 收集器 collectors
2023-06-13 09:15:10 时间
文章目录
Code
public class CollectorsAction {
public static List<Dish> menu = Arrays.asList(
new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
new Dish("french fries", true, 530, Dish.Type.OTHER),
new Dish("rice", true, 350, Dish.Type.OTHER),
new Dish("season fruit", true, 120, Dish.Type.OTHER),
new Dish("pizza", true, 550, Dish.Type.OTHER),
new Dish("prawns", false, 300, Dish.Type.FISH),
new Dish("salmon", false, 450, Dish.Type.FISH));
public static void main(String[] args) {
testAveragingDouble();
testAveragingInt();
testAveragingLong();
testCollectingAndThen();
testCounting();
testGroupingByFunction();
testGroupingByFunctionAndCollector();
testGroupingByFunctionAndSupplierAndCollector();
testSummarizingInt();
testGroupingByConcurrentWithFunction();
testGroupingByConcurrentWithFunctionAndCollector();
testGroupingByConcurrentWithFunctionAndSupplierAndCollector();
testJoining();
testJoiningWithDelimiter();
testJoiningWithDelimiterAndPrefixAndSuffix();
testMapping();
testMaxBy();
testMinBy();
testPartitioningByWithPredicate();
testPartitioningByWithPredicateAndCollector();
testReducingBinaryOperator();
testReducingBinaryOperatorAndIdentiy();
testReducingBinaryOperatorAndIdentiyAndFunction();
testSummarizingDouble();
testSummarizingLong();
testSummarizingInt();
}
private static void testAveragingDouble() {
System.out.println("testAveragingDouble");
Optional.ofNullable(menu.stream().collect(Collectors.averagingDouble(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testAveragingInt() {
System.out.println("testAveragingInt");
Optional.ofNullable(menu.stream().collect(Collectors.averagingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testAveragingLong() {
System.out.println("testAveragingLong");
Optional.ofNullable(menu.stream().collect(Collectors.averagingLong(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testCollectingAndThen() {
System.out.println("testCollectingAndThen");
Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.averagingInt(Dish::getCalories), a -> "The Average Calories is->" + a)))
.ifPresent(System.out::println);
/* List<Dish> list = menu.stream().filter(d -> d.getType().equals(Dish.Type.MEAT)) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); list.add(new Dish("", false, 100, Dish.Type.OTHER)); System.out.println(list);*/
}
private static void testCounting() {
System.out.println("testCounting");
Optional.of(menu.stream().collect(Collectors.counting())).ifPresent(System.out::println);
}
private static void testGroupingByFunction() {
System.out.println("testGroupingByFunction");
Optional.of(menu.stream().collect(Collectors.groupingBy(Dish::getType)))
.ifPresent(System.out::println);
}
private static void testGroupingByFunctionAndCollector() {
System.out.println("testGroupingByFunctionAndCollector");
Optional.of(menu.stream().collect(Collectors.groupingBy(Dish::getType, Collectors.averagingInt(Dish::getCalories))))
.ifPresent(System.out::println);
}
private static void testGroupingByFunctionAndSupplierAndCollector() {
System.out.println("testGroupingByFunctionAndSupplierAndCollector");
Map<Dish.Type, Double> map = menu.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new, Collectors.averagingInt(Dish::getCalories)));
Optional.of(map.getClass()).ifPresent(System.out::println);
Optional.of(map).ifPresent(System.out::println);
}
private static void testSummarizingInt() {
System.out.println("testSummarizingInt");
IntSummaryStatistics result = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
Optional.of(result).ifPresent(System.out::println);
}
private static void testGroupingByConcurrentWithFunction() {
System.out.println("testGroupingByConcurrentWithFunction");
ConcurrentMap<Dish.Type, List<Dish>> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType));
Optional.ofNullable(collect.getClass()).ifPresent(System.out::println);
Optional.ofNullable(collect).ifPresent(System.out::println);
}
private static void testGroupingByConcurrentWithFunctionAndCollector() {
System.out.println("testGroupingByConcurrentWithFunctionAndCollector");
ConcurrentMap<Dish.Type, Double> collect = menu.stream()
.collect(Collectors.groupingByConcurrent(Dish::getType, Collectors.averagingInt(Dish::getCalories)));
Optional.ofNullable(collect).ifPresent(System.out::println);
}
private static void testGroupingByConcurrentWithFunctionAndSupplierAndCollector() {
System.out.println("testGroupingByConcurrentWithFunctionAndSupplierAndCollector");
ConcurrentMap<Dish.Type, Double> collect = menu.stream()
.collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingInt(Dish::getCalories)));
Optional.of(collect.getClass()).ifPresent(System.out::println);
Optional.ofNullable(collect).ifPresent(System.out::println);
}
private static void testJoining() {
System.out.println("testJoining");
Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining()))
.ifPresent(System.out::println);
}
private static void testJoiningWithDelimiter() {
System.out.println("testJoiningWithDelimiter");
Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining(",")))
.ifPresent(System.out::println);
}
private static void testJoiningWithDelimiterAndPrefixAndSuffix() {
System.out.println("testJoiningWithDelimiterAndPrefixAndSuffix");
Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining(",", "Names[", "]")))
.ifPresent(System.out::println);
}
private static void testMapping() {
System.out.println("testMapping");
Optional.of(menu.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(","))))
.ifPresent(System.out::println);
}
private static void testMaxBy() {
System.out.println("testMaxBy");
menu.stream().collect(Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testMinBy() {
System.out.println("testMinBy");
menu.stream().collect(Collectors.minBy(Comparator.comparingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testPartitioningByWithPredicate() {
System.out.println("testPartitioningByWithPredicate");
Map<Boolean, List<Dish>> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian));
Optional.of(collect).ifPresent(System.out::println);
}
private static void testPartitioningByWithPredicateAndCollector() {
System.out.println("testPartitioningByWithPredicateAndCollector");
Map<Boolean, Double> collect = menu.stream()
.collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.averagingInt(Dish::getCalories)));
Optional.of(collect).ifPresent(System.out::println);
}
private static void testReducingBinaryOperator() {
System.out.println("testReducingBinaryOperator");
menu.stream().collect(
Collectors.reducing(
BinaryOperator.maxBy(
Comparator.comparingInt(Dish::getCalories)
)
)
).ifPresent(System.out::println);
}
private static void testReducingBinaryOperatorAndIdentiy() {
System.out.println("testReducingBinaryOperatorAndIdentiy");
Integer result = menu.stream()
.map(Dish::getCalories).collect(Collectors.reducing(0, (d1, d2) -> d1 + d2));
System.out.println(result);
}
private static void testReducingBinaryOperatorAndIdentiyAndFunction() {
System.out.println("testReducingBinaryOperatorAndIdentiyAndFunction");
Integer result = menu.stream().collect(Collectors.reducing(0, Dish::getCalories, (d1, d2) -> d1 + d2));
System.out.println(result);
}
private static void testSummarizingDouble() {
System.out.println("testSummarizingDouble");
Optional.of(menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testSummarizingLong() {
System.out.println("testSummarizingLong");
Optional.of(menu.stream().collect(Collectors.summarizingLong(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testSummarizingInt() {
System.out.println("testSummarizingLong");
Optional.of(menu.stream().collect(Collectors.summarizingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/234940.html原文链接:https://javaforall.cn
相关文章
- JAVA多线程面试题_java多线程的实现方式
- Java IO 和 NIO的区别
- java calendar 设置小时_Java Calendar.set 方法设置时间的问题
- eclipse怎么导入java文件_Eclipse如何导入JAVA工程?如何将项目导入Eclipse中?
- java uuid 随机数_Java随机数和UUID[通俗易懂]
- 【说站】java短路逻辑运算符是什么
- Java调用第三方平台发送手机短信
- 使用Java、Matlab画多边形闭合折线图详解编程语言
- java 标准输出与标准错误 out与 err 区别 用法 联系 java中的out与err区别 System.out和System.err的区别 System.out.println和System.err.println的区别 Java重定向System.out和System.err详解编程语言
- Java项目实战之图书馆管理系统(带源码和解析)
- 技巧Linux环境下提高Java编译效率的技巧(linux下java编译)
- Java与Oracle联手构建数据库新世界(java和oracle)
- Java编程操作Oracle数据库(java操作oracle)
- Linux下安装Java的简易步骤(linux如何安装java)
- 处理实践Redis中Java对过期键的处理实战(redisjava过期)
- Java玩转Redis入门指南(redis入门Java)
- 国内首本系统讲解Java异步编程的书籍-java异步编程实战
- 实现Java实现Redis锁的研究与应用(redis锁java)
- Oracle与Java程序融合谱写成功曲(java程序Oracle)
- 浅析java中Integer传参方式的问题
- java通过复选框控件数组实现添加多个复选框控件示例分享
- java计算自幂数和水仙花数