JDK 8中引入的三个新类是java.util包的DoubleSummaryStatistics , IntSummaryStatistics和LongSummaryStatistics 。 这些类使计算元素总数,元素最小值,元素最大值,元素平均值以及双精度,整数或long的集合中的元素总和变得轻松快捷。 每个类的类级别Javadoc文档都以相同的单句开头,简洁地表达了这一点,并将每个句子描述为“用于收集统计信息(例如,计数,最小值,最大值,总和和平均值)的状态对象”。

这三个类中每一个的类级Javadoc都声明了每个类,“该类旨在用于(尽管不需要)流。” 包含这三种类型的SummaryStatistics类的最明显的原因是要与JDK 8一起引入的流一起使用。

实际上,三个类的类级别Javadoc注释中的每个注释也提供了将每个类与相应数据类型的流结合使用的示例。 这些示例演示了调用各个Stream的collect(Supplier,BiConsumer,BiConsumer)方法( 可变归约 终端流操作 )并将每个SummaryStatistics类的新实例 (构造函数)传递给accept方法, 并将方法(作为方法引用 )传递给此collect方法作为其“供应商”,“累加器”和“合并器”参数。

本文的其余部分将演示IntSummaryStatisticsLongSummaryStatisticsDoubleSummaryStatistics 。 这些示例中的几个示例将参考X-Files电视连续剧的各个季节的地图,以该季节首映的Nielsen评分为参考。 这显示在下一个代码清单中。

声明和初始化xFilesSeasonPremierRatings

/*** Maps the number of each X-Files season to the Nielsen rating* (millions of viewers) for the premiere episode of that season.*/
private final static Map<Integer, Double> xFilesSeasonPremierRatings;static
{final Map<Integer, Double> temporary = new HashMap<>();temporary.put(1, 12.0);temporary.put(2, 16.1);temporary.put(3, 19.94);temporary.put(4, 21.11);temporary.put(5, 27.34);temporary.put(6, 20.24);temporary.put(7, 17.82);temporary.put(8, 15.87);temporary.put(9, 10.6);xFilesSeasonPremierRatings = Collections.unmodifiableMap(temporary);
}

下一个代码清单使用在上一个代码清单中创建的映射,演示将DoubleSummaryStatistics应用于DoubleSummaryStatistics的“值”部分的流,并且与Javadoc中为三个SummaryStatistics类提供的示例非常相似。 DoubleSummaryStatistics类, IntSummaryStatistics类和LongSummaryStatistics类具有基本相同的字段,方法和API(仅差异是受支持的数据类型)。 因此,即使本示例以及本示例中的许多示例都专门使用DoubleSummaryStatistics (因为X文件的Nielsen评分是两倍),该原理仍适用于SummaryStatistics类的其他两个不可或缺的类型。

将DoubleSummaryStatistics与基于集合的流一起使用

/*** Demonstrate use of DoubleSummaryStatistics collected from a* Collection Stream via use of DoubleSummaryStatistics method* references "new", "accept", and "combine".*/
private static void demonstrateDoubleSummaryStatisticsOnCollectionStream()
{final DoubleSummaryStatistics doubleSummaryStatistics =xFilesSeasonPremierRatings.values().stream().collect(DoubleSummaryStatistics::new,DoubleSummaryStatistics::accept,DoubleSummaryStatistics::combine);out.println("X-Files Season Premieres: " + doubleSummaryStatistics);
}

接下来显示运行上述演示的输出:

X-Files Season Premieres: DoubleSummaryStatistics{count=9, sum=161.020000, min=10.600000, average=17.891111, max=27.340000}

上一个示例直接基于集合( Map的“值”部分)将SummaryStatistics类应用于流。 下一个代码清单演示了一个类似的示例,但是使用IntSummaryStatistics并使用流的中间映射操作来指定在集合的对象上调用哪个函数以填充SummaryStatistics对象。 在这种情况下,由Java8StreamsMoviesDemo.getMoviesSample()方法返回的Set<Movie>对集合进行操作,并在我的博客文章JDK 8中的Stream-Powered Collections Functionality中进行了详细说明 。

将IntSummaryStatistics与Stream的地图一起使用(功能)

/*** Demonstrate collecting IntSummaryStatistics via mapping of* certain method calls on objects within a collection and using* lambda expressions (method references in particular).*/
private static void demonstrateIntSummaryStatisticsWithMethodReference()
{final Set<Movie> movies = Java8StreamsMoviesDemo.getMoviesSample();IntSummaryStatistics intSummaryStatistics =movies.stream().map(Movie::getImdbTopRating).collect(IntSummaryStatistics::new, IntSummaryStatistics::accept, IntSummaryStatistics::combine);out.println("IntSummaryStatistics on IMDB Top Rated Movies: " + intSummaryStatistics);
}

执行上面的演示时,其输出如下所示:

IntSummaryStatistics on IMDB Top Rated Movies: IntSummaryStatistics{count=5, sum=106, min=1, average=21.200000, max=49}

到目前为止,这些示例已在其最常用的情况下(与基于现有集合的流中的数据结合使用)使用SummaryStatistics类进行了演示。 下面的例子演示了如何DoubleStream可以从头开始通过利用被实例化DoubleStream.Builder然后DoubleStream的summaryStatistics()方法可以被调用来获得的实例DoubleSummaryStatistics

从DoubleStream获取DoubleSummaryStatistics的实例

/*** Uses DoubleStream.builder to build an arbitrary DoubleStream.** @return DoubleStream constructed with hard-coded doubles using*    a DoubleStream.builder.*/
private static DoubleStream createSampleOfArbitraryDoubles()
{return DoubleStream.builder().add(12.4).add(13.6).add(9.7).add(24.5).add(10.2).add(3.0).build();
}/*** Demonstrate use of an instance of DoubleSummaryStatistics* provided by DoubleStream.summaryStatistics().*/
private static void demonstrateDoubleSummaryStatisticsOnDoubleStream()
{final DoubleSummaryStatistics doubleSummaryStatistics =createSampleOfArbitraryDoubles().summaryStatistics();out.println("'Arbitrary' Double Statistics: " + doubleSummaryStatistics);
}

刚列出的代码产生以下输出:

'Arbitrary' Double Statistics: DoubleSummaryStatistics{count=6, sum=73.400000, min=3.000000, average=12.233333, max=24.500000}

当然,类似于刚刚所示的例子中, IntStream和IntStream.Builder可以提供的一个实例IntSummaryStatistics和LongStream和LongStream.Builder可以提供的一个实例LongSummaryStatistics

一个人不需要拥有StreamStream或BaseStream的其他实例即可使用SummaryStatistics类,因为它们可以直接实例化并直接用于预定义的数字统计操作。 下一个代码清单通过直接实例化然后填充DoubleSummaryStatistics的实例来演示这DoubleSummaryStatistics

直接实例化DoubleSummaryStatistics

/*** Demonstrate direct instantiation of and population of instance* of DoubleSummaryStatistics instance.*/
private static void demonstrateDirectAccessToDoubleSummaryStatistics()
{final DoubleSummaryStatistics doubleSummaryStatistics =new DoubleSummaryStatistics();doubleSummaryStatistics.accept(5.0);doubleSummaryStatistics.accept(10.0);doubleSummaryStatistics.accept(15.0);doubleSummaryStatistics.accept(20.0);out.println("Direct DoubleSummaryStatistics Usage: " + doubleSummaryStatistics);
}

接下来显示运行先前代码清单的输出:

Direct DoubleSummaryStatistics Usage: DoubleSummaryStatistics{count=4, sum=50.000000, min=5.000000, average=12.500000, max=20.000000}

就像上一个DoubleSummaryStatistics代码清单中DoubleSummaryStatistics ,下一个代码清单直接实例化LongSummaryStatistics并将其填充)。 此示例还演示了SummaryStatistics类如何提供用于请求单个统计信息的单个方法。

直接实例化LongSummaryStatistics /请求单个统计信息

/*** Demonstrate use of LongSummaryStatistics with this particular* example directly instantiating and populating an instance of* LongSummaryStatistics that represents hypothetical time* durations measured in milliseconds.*/
private static void demonstrateLongSummaryStatistics()
{// This is a series of longs that might represent durations// of times such as might be calculated by subtracting the// value returned by System.currentTimeMillis() earlier in// code from the value returned by System.currentTimeMillis()// called later in the code.LongSummaryStatistics timeDurations = new LongSummaryStatistics();timeDurations.accept(5067054);timeDurations.accept(7064544);timeDurations.accept(5454544);timeDurations.accept(4455667);timeDurations.accept(9894450);timeDurations.accept(5555654);out.println("Test Results Analysis:");out.println("\tTotal Number of Tests: " + timeDurations.getCount());out.println("\tAverage Time Duration: " + timeDurations.getAverage());out.println("\tTotal Test Time: " + timeDurations.getSum());out.println("\tShortest Test Time: " + timeDurations.getMin());out.println("\tLongest Test Time: " + timeDurations.getMax());
}

现在显示此示例的输出:

Test Results Analysis:Total Number of Tests: 6Average Time Duration: 6248652.166666667Total Test Time: 37491913Shortest Test Time: 4455667Longest Test Time: 9894450

在本文的大多数示例中,我都依赖SummaryStatistics类的可读toString()实现来演示每个类中可用的统计信息。 但是,最后一个示例说明,每种单独的统计信息类型(值的数量,最大值,最小值,值的总和和平均值)都可以以数字形式分别检索。

结论

无论所分析的数据是直接作为数字流提供,还是通过集合的流间接提供,还是手动放置在适当的SummaryStatistics类实例中,这三个SummaryStatistics类都可以提供有关整数,长整数和双精度数的有用的常用统计计算。

翻译自: https://www.javacodegeeks.com/2015/04/the-jdk-8-summarystatistics-classes.html

JDK 8 SummaryStatistics类相关推荐

  1. Java 使用 endorsed 覆盖jdk提供的类

    在分析 Tomcat catalina.bat 原理解析时候,我们发现在启动tomcat的参数中存在 -Djava.endorsed.dirs 参数 如下图: -Djava.endorsed.dirs ...

  2. 14.JDK底层Unsafe类是个啥东西?

    老王:小陈啊,从今天开始我们就要进入<结丹篇>了,在这一篇章里面,要注意听讲啊,对后面的每一个阶段的理解来说都至关重要的...... 小陈:好的,老王,前面的<筑基>.< ...

  3. endorsed java_Java中jdk提供的类怎么利用endorsed进行覆盖

    Java中jdk提供的类怎么利用endorsed进行覆盖 发布时间:2020-12-03 16:44:03 来源:亿速云 阅读:70 作者:Leah 这篇文章将为大家详细讲解有关Java中jdk提供的 ...

  4. endorsed java_Java利用endorsed如何覆盖jdk提供的类详解

    前言 在之前我们分析 Tomcat catalina.bat 原理解析 时候,我们发现在启动tomcat的参数中存在 -Djava.endorsed.dirs 参数 如下图: -Djava.endor ...

  5. 为什么JDK中String类的indexof不使用KMP或者Boyer-Moore等时间复杂度低的算法编辑器

    indexOf底层使用的方法是典型的BF算法. 1.KMP算法 由来 外国人: Knuth,Morris和Pratt发明了这个算法,然后取它三个的首字母进行了命名.所以叫做KMP. KMP真的很难理解 ...

  6. jdk自带类实现json解析

    JSON简介 JAVAScript Object Notation是一种轻量级的数据交换格式 具有良好的可读和便于快速编写的特性. 业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了 ...

  7. java超级计算器,jdk自带类

    2019独角兽企业重金招聘Python工程师标准>>> package lcr;import java.math.BigInteger;/*** 超级计算器* * @author L ...

  8. java观察者模式类图_设计模式(十八)——观察者模式(JDK Observable源码分析)...

    1 天气预报项目需求,具体要求以下: 1) 气象站能够将天天测量到的温度,湿度,气压等等以公告的形式发布出去(好比发布到本身的网站或第三方).java 2) 须要设计开放型 API,便于其余第三方也能 ...

  9. BigInteger类实例的构造过程——JDK源码解析

    最近看了下JDK1.6版本的BigInteger类,仔细研究了下大整数实例的构造过程,现在把自己的所得所想分享给大家. 首先,为什么需要大整数类?简单的说就是因为内部的数据类型能表示的最大数是64位长 ...

最新文章

  1. IIS7.5 HTTP 错误 500 调用loadlibraryex失败的解决方法
  2. PostgreSQL调研
  3. 很抱歉,这场大会我们没法卖票给你了
  4. WPF学习一--概述
  5. gRPC 基础概念详解
  6. java序列化写法_java-spark的各种常用算子的写法
  7. Intel 64/x86_64/IA-32/x86处理器 - 指令格式(1) - 概述
  8. java spark es_ES-Spark连接ES后,ES Client节点流量打满分析
  9. GitHub 标星 11000+,阿里开源的微服务组件如何连续 10 年扛住双十一大促?
  10. Objective-C 和 Core Foundation 对象相互转换的内存管理总结
  11. struts2进阶篇(3)
  12. Java开发二维码扫一扫名片技术
  13. js工具库Ramda和lodash和underscore用法对比
  14. 生物群落多样性——β多样性
  15. becon帧 wifi_构造并发送Beacon帧以伪造任意WiFi热点
  16. 没错,我真的拒了蚂蚁金服的Offer
  17. 决赛名单出炉!“云”上巅峰群雄竞
  18. 基于stm32的两轮自平衡小车3(硬件篇)
  19. 题目 1180: 不容易系列
  20. HTML+CSS网页制作——人生指南

热门文章

  1. Hibernate中使用Criteria查询及注解——(hibernate.cfg.xml)
  2. 2018蓝桥杯省赛---java---B---6(递增三元组)
  3. 西瓜显示服务器错误,西瓜云服务器
  4. JDBC连接数据库教程,postgreSQL
  5. line和spline_探索适用于Apache Spark的Spline Data Tracker和可视化工具(第1部分)
  6. aws lambda使用_使用AWS Lambdas扩展技术堆栈
  7. 微服务java模块内存管理_Java 9模块服务
  8. 队列和消息队列_消息队列概述[幻灯片]
  9. jconsole查看连接数_在JConsole和VisualVM中查看DiagnosticCommandMBean
  10. java中的方法求和_在Java中模拟求和类型的巧妙解决方法