Java 中的 IntStream forEach() 方法


forEach() 方法用於 Java 來對這個流的每個元素執行操作。使用 forEach() 方法在 Java IntStream 中顯示流

語法如下

void forEach(IntConsumer action)

這裡,action 是在元素上執行的非干擾操作。

建立 IntStream 並新增元素

IntStream intStream = IntStream.of(15, 30, 40, 60, 75, 90);

現在,顯示流

intStream.forEach(System.out::println);

以下是實現 Java 中 IntStream forEach() 方法的示例

示例

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(15, 30, 40, 60, 75, 90);
      intStream.forEach(System.out::println);
   }
}

輸出

15
30
40
60
75
90

更新日期: 30-Jul-2019

398 瀏覽

開啟您的職業生涯

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.