IntStream peek() 方法在 Java 中


IntStream 類的 peek() 方法在 Java 中返回一個由該流的元素組成的流。它還針對從結果流中提取的每個元素執行提供的操作。

語法如下

IntStream peek(IntConsumer action)

在此,引數 action 是從流中提取元素時要執行的非干擾操作。IntConsumer 表示接受單個 int 值作為引數且不返回任何結果的操作。

以下示例在 Java 中實現了 IntStream peek() 方法

示例

 演示

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(50, 60);
      System.out.println("Elements in the stream = ");
      long res = intStream.peek(System.out::println).count();
      System.out.println("Count of elements = " + res);
   }
}

輸出

Elements in the stream =
50
51
52
53
54
55
56
57
58
59
Count of elements = 10

更新於: 30-Jul-2019

599 次瀏覽

開啟您的 職業生涯

透過完成本課程獲得認證

開始
廣告
© . All rights reserved.