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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

JAVA8 Stream方法使用详解reduce、IntStream(二)

發(fā)布時間:2025/1/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA8 Stream方法使用详解reduce、IntStream(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 一 歸約
      • 1、元素求和
      • 2、最大值和最小值
    • 二、數(shù)值流
      • 1、映射數(shù)值流
      • 2、轉換對象流
      • 3、數(shù)值范圍
    • 三、構建流
      • 1、由值創(chuàng)建流
      • 2、由數(shù)組創(chuàng)建流
      • 3、由文件生成流
      • 4、由函數(shù)生成流

此章節(jié)繼續(xù)介紹其它Stream API用法

一 歸約

歸約將流中的所有元素反復結合起來,歸約成一個值。用函數(shù)式的術語來說,可以稱為折疊。

1、元素求和

reduce接受兩個參數(shù):
1、一個是初始值
2、一個是BinaryOperator將兩個元素結合成一個新值,比如:(a,b) -> a+b
舉例求和:

public static void testReduce() {List<Integer> integers = Arrays.asList(1, 2, 3, 45, 6);Integer reduce = integers.stream().reduce(0, (a, b) -> a + b);System.out.println(reduce);}

reduce還有個重載方法不接受初始值,返回的是一個Optional。

2、最大值和最小值

給定兩個元素返回一個最大值的lambda,reduce會考慮新值和流中的下一個元素,并產(chǎn)生一個最大值,直到流消費結束。
舉例說明:

public static void testReduceMax() {List<Integer> integers = Arrays.asList(1, 2, 3, 45, 6);Optional<Integer> reduce = integers.stream().reduce(Integer::max);System.out.println(reduce.get());Optional<Integer> reduce1 = integers.stream().reduce(Integer::min);System.out.println(reduce1.get());}

使用reduce的好處,迭代被內(nèi)部迭代抽象了,內(nèi)部實現(xiàn)得以選擇并行執(zhí)行reduce操作。

二、數(shù)值流

使用reduce可以計算流中元素的總和,但是這樣是有問題,有一個暗含的裝箱成本,每個integer都必須拆箱成一個原始類型,再求和,可以直接調(diào)用sum:

public static void testReduceSum() {List<Integer> integers = Arrays.asList(1, 2, 3, 45, 6);int sum = integers.stream().mapToInt(d -> d.intValue()).sum();System.out.println(sum);}

1、映射數(shù)值流

java8引入了三個原始類型流解決裝箱拆箱問題:IntStream、DoubleStream、LongStream。每個接口中都有對數(shù)sum,max,min,average等方法。
例:

public static void testReduceSum() {List<Integer> integers = Arrays.asList(1, 2, 3, 45, 6);int sum = integers.stream().mapToInt(d -> d.intValue()).sum();OptionalInt max = integers.stream().mapToInt(d -> d.intValue()).max();OptionalInt min = integers.stream().mapToInt(d -> d.intValue()).min();OptionalDouble average = integers.stream().mapToInt(d -> d.intValue()).average();System.out.println(sum);}

2、轉換對象流

將數(shù)值流轉化成對象流調(diào)用方法,調(diào)用boxed方法。

public static void testReduceObj() {List<Integer> integers = Arrays.asList(1, 2, 3, 45, 6);IntStream intStream = integers.stream().mapToInt(d -> d.intValue());Stream<Integer> boxed = intStream.boxed();}

3、數(shù)值范圍

IntStream、DoubleStream、LongStream生成流范圍range不包含結束值,rangeClosed包含結束值。

public static void testRangeClosed() {IntStream intStream = IntStream.rangeClosed(0, 100).filter(d -> d % 2 == 0);long count = intStream.count();System.out.println(count);}

三、構建流

創(chuàng)建流的方法有很多,值序列、數(shù)據(jù)組、文件創(chuàng)建流。

1、由值創(chuàng)建流

靜態(tài)方法Stream.of。例:

public static void testStreamof() {Stream<String> java8 = Stream.of("java8", "lambda", "in action");} }

2、由數(shù)組創(chuàng)建流

Arrays.stream

int[] i = new int[]{1,3,34};IntStream stream = Arrays.stream(i);

3、由文件生成流

java中用于處理文件的I/O操作已更新,以便用Stream API。
java.nio.file.Files中有很多靜態(tài)方法會返回一個stream。一個很有用的方法Files.lines。例:

Stream<String> lines = Files.lines(Paths.get("xx.txt"));

4、由函數(shù)生成流

Stream.iterate和Stream.generate可以創(chuàng)建無限流。

Stream.iterate,iterate接受一個初始值為0,接受參數(shù)(final T seed, final UnaryOperator f)
例:

public static void testiterate() {Stream<Integer> iterate = Stream.iterate(0, n -> n + 2);}

此方法生成一個正偶數(shù)的流。

Stream.generate,generate也可以生成一個無限流,但generate不是依次 對每個新生成的值應用函數(shù)。接受參數(shù)Supplier s

Stream<Double> generate = Stream.generate(Math::random);

總結

以上是生活随笔為你收集整理的JAVA8 Stream方法使用详解reduce、IntStream(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。