📜  Java中的LongStream of()

📅  最后修改于: 2022-05-13 01:55:32.570000             🧑  作者: Mango

Java中的LongStream of()

LongStream of(long t)

LongStream of(long t)返回包含单个元素的顺序 LongStream。
句法 :

static LongStream of(long t)

参数 :

  1. LongStream :原始长值元素的序列。
  2. t :表示 LongStream 中的单个元素。

返回值: LongStream of(long t) 返回包含单个指定元素的顺序 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(long… values)

LongStream of(long... values)返回一个顺序有序的流,其元素是指定的值。
句法 :

static LongStream of(long... values)

参数 :

  1. LongStream :原始长值元素的序列。
  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