Java 中 IntStream 的 count() 方法


Java 中 IntStream 類的 count() 方法返回此流中的元素個數

語法如下

long count()

首先,建立一個 IntStream 並新增一些元素

IntStream intStream = IntStream.of(30, 13, 67, 56, 89, 99, 76, 56);

現在,使用 count() 方法獲取流的元素個數

long num = intStream.count();

下面是一個在 Java 中實現 IntStream count() 方法的示例

示例

 線上示例

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(30, 13, 67, 56, 89, 99, 76, 56);
      long num = intStream.count();
      System.out.println("Count of elements in the stream: "+num);
   }
}

輸出

Count of elements in the stream: 8

更新日期:2019 年 7 月 30 日

112 次觀看

開啟您的職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.