文章目录

  • Code


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);}
}

Java 8 - 收集器Collectors_实战相关推荐

  1. Java 8 - 收集器Collectors_归约和汇总

    文章目录 Pre 查找流中的最大值和最小值 需求:想要找出热量最高的菜和热量最低的菜 汇总 需求: 求出菜单列表的总热量 需求: 一次操作求出菜单中元素的个数,并得总和.平均值.最大值和最小值 (su ...

  2. Java 8 - 收集器Collectors_分组groupingBy

    文章目录 Pre 多级分组 按子组收集数据 查找每个子组中热量最高的 Dish 图解工作过程 与 groupingBy联合使用的其他收集器的例子 附 Pre 来看个小例子: 把菜单中的菜按照类型进行分 ...

  3. Java 8 - 收集器Collectors_分区partitioningBy

    文章目录 概述 Demo 概述 分区是分组的特殊情况:由一个谓词(返回一个布尔值的函数)作为分类函数,它称分区函数 . 分区函数返回一个布尔值,这意味着得到的分组 Map 的键类型是 Boolean ...

  4. 深入JVM虚拟机(四) Java GC收集器

    转载自  深入JVM虚拟机(四) Java GC收集器 1 GC收集器 1.1 Serial串行收集器 串行收集器主要有两个特点:第一,它仅仅使用单线程进行垃圾回收:第二,它独占式的垃圾回收. 在串行 ...

  5. Java GC收集器配置说明

    根据Java GC收集器具体分类,我们可以看出JVM根据需求不同提供了三种选择:串行收集器.并行收集器.并发收集器. 串行收集器只适用于小数据量的情况,我们主要了解一下并行收集器和并发收集器.默认情况 ...

  6. java虚拟机收集器_Java虚拟机(JVM)垃圾回收器G1收集器 - Break易站

    G1收集器 G1(Garbage-First)是JDK7-u4才推出商用的收集器: 1.特点 (A).并行与并发 能充分利用多CPU.多核环境下的硬件优势: 可以并行来缩短"Stop The ...

  7. Java 8 - 收集器Collectors

    文章目录 Pre 简介 收集器用作高级归约 预定义收集器 Pre 我们前面学到了,流可以用类似于数据库的操作帮助你处理集合. 它们支持两种类型的操作: 中间操作(如 filter 或 map ) 终端 ...

  8. java默认收集器_jvm默认垃圾收集器

    jdk1.7 默认垃圾收集器Parallel Scavenge(新生代)+Parallel Old(老年代) jdk1.8 默认垃圾收集器Parallel Scavenge(新生代)+Parallel ...

  9. Java 8系列之重构和定制收集器

    Stream系列: Java 8系列之Stream的基本语法详解 Java 8系列之Stream的强大工具Collector Java 8系列之重构和定制收集器 Java 8系列之Stream中万能的 ...

最新文章

  1. C语言结束输入(两种方法)
  2. 代金券制作小程序秒代金券_微信小程序制作工具与方法
  3. 有关C语言中有符号/无符号数混合运算的小问题
  4. linux 安装python-opencv
  5. Hash表的扩容(转载)
  6. DELPHI之备忘(二)
  7. Win10系统电脑不会一键还原系统怎么解决
  8. matplotlib绘制图形
  9. html - table分页断行,关于window.print网页分页换页table不断行的处理
  10. c++ poco 使用mysql中文乱码问题
  11. foxmail 7.2密码查看工具_MacOS装机必备:Archiver 3 for Mac解压缩工具
  12. react在线文件_在线IDE开发入门之从零实现一个在线代码编辑器
  13. 联想昭阳E42-80高通QCA9377安裝Ubuntu14.04无法使用Wi-Fi解決方法
  14. 使用网易云api、Vue 和swiper实现轮播图 (音乐app项目-第2步)
  15. 实时查看Starlink在轨卫星、地面站数目和分布情况的有趣网站
  16. 多重检验_LSD方法不准确性
  17. 国庆专属头像、国旗专属头像一键生成源代码
  18. 从外包月薪5K到阿里月薪15K,大厂面试必备技能
  19. Mysql information_schema库
  20. Kubernetes 1.5通过Ceph实现有状态容器

热门文章

  1. C++虚继承时的构造函数
  2. C++多继承(多重继承)详解(二)命名冲突
  3. 131. Leetcode 191. 位1的个数 (位运算-汉明距离相关题目)
  4. 推荐系统笔记:基于贝叶斯的协同过滤
  5. MATLAB实战系列(二十六)-matlab通过遗传算法求解车间调度问题
  6. 精准评论是如何在娱乐类产品中大放异彩?
  7. Scrapy框架学习记录
  8. 【android4.3】记一次完整的android源码截屏事件的捕获(不同于网上的老版本)
  9. 第4章 最基础的分类算法-k近邻算法
  10. 花果山第一届猿类分级考试实录--Talk is cheap,Show me the code