當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LongStream of()用法及代碼示例


LongStream of(long t)

LongStream of(long t)返回包含單個元素的順序LongStream。
用法:

static LongStream of(long t)

參數:

  1. LongStream : 一係列原始long值元素。
  2. t : 表示LongStream中的單個元素。

返回值:(long t)的LongStream返回包含單個指定元素的順序LongStream。


例:

// Java code for LongStream of(long t) 
// to get a sequential LongStream 
// containing a single element. 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream having single element only 
        LongStream stream = LongStream.of(-7L); 
  
        // Displaying the LongStream having single element 
        stream.forEach(System.out::println); 
    } 
}
輸出:
-7

LongStream of(長…值)

LongStream of(長…值)返回其元素為指定值的順序有序流。
用法:

static LongStream of(long... values)

參數:

  1. LongStream : 一係列原始long值元素。
  2. values : 表示新流的元素。

返回值:LongStream of(long ... values)返回一個順序有序的流,其元素為指定值。

示例1:

// Java code for LongStream of(long... values) 
// to get a sequential ordered stream whose 
// elements are the specified values. 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream 
        LongStream stream = LongStream.of(-7L, -9L, -11L); 
  
        // Displaying the sequential ordered stream 
        stream.forEach(System.out::println); 
    } 
}
輸出:
-7
-9
-11

示例2:

// Java code for LongStream of(long... values) 
// to get a sequential ordered stream whose 
// elements are the specified values. 
import java.util.*; 
import java.util.stream.LongStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an LongStream 
        LongStream stream = LongStream.of(-7L, -9L, -11L); 
  
        // Storing the count of elements in LongStream 
        long total = stream.count(); 
  
        // Displaying the count of elements 
        System.out.println(total); 
    } 
}
輸出:
3


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 LongStream of() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。