日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDK 8 SummaryStatistics类

發布時間:2023/12/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDK 8 SummaryStatistics类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JDK 8中引入的三個新類是java.util包的DoubleSummaryStatistics , IntSummaryStatistics和LongSummaryStatistics 。 這些類使計算元素總數,元素最小值,元素最大值,元素平均值以及雙精度,整數或long的集合中的元素總和變得輕松快捷。 每個類的類級別Javadoc文檔都以相同的單句開頭,簡潔地表達了這一點,并將每個句子描述為“用于收集統計信息(例如,計數,最小值,最大值,總和和平均值)的狀態對象”。

這三個類中每一個的類級Javadoc都聲明了每個類,“該類旨在用于(盡管不需要)流。” 包含這三種類型的SummaryStatistics類的最明顯的原因是要與JDK 8一起引入的流一起使用。

實際上,三個類的類級別Javadoc注釋中的每個注釋也提供了將每個類與相應數據類型的流結合使用的示例。 這些示例演示了調用各個Stream的collect(Supplier,BiConsumer,BiConsumer)方法( 可變歸約 終端流操作 )并將每個SummaryStatistics類的新實例 (構造函數)傳遞給accept方法, 并將方法(作為方法引用 )傳遞給此collect方法作為其“供應商”,“累加器”和“合并器”參數。

本文的其余部分將演示IntSummaryStatistics , LongSummaryStatistics和DoubleSummaryStatistics 。 這些示例中的幾個示例將參考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类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。