📜  Java中的 Stream.of(T... values) 示例

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

Java中的 Stream.of(T... values) 示例

Stream of(T... values)返回一个顺序有序的流,其元素是指定的值。顺序流的工作方式与使用单核的 for 循环类似。另一方面,并行流将提供的任务分成许多,并利用计算机的多个内核在不同的线程中运行它们。

句法 :

static  Stream of(T... values)

Where, Stream is an interface and T
is the type of stream elements.
values represents the elements of the 
new stream and the function 
returns the new stream.

示例 1:字符串流。

// Java code for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream of Strings
        // and printing sequential
        // ordered stream
        Stream stream = Stream.of("Geeks", "for", "Geeks");
  
        stream.forEach(System.out::println);
    }
}

输出 :

Geeks
for
Geeks

示例 2:整数流。

// Java code for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream of Integer
        // and printing sequential
        // ordered stream
        Stream stream = Stream.of(5, 7, 9, 12);
  
        stream.forEach(System.out::println);
    }
}

输出 :

5
7
9
12

示例 3: Long 原语流。

// Java code for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream of Long
        // and printing sequential
        // ordered stream
        Stream stream = Stream.of(4L, 8L, 12L, 16L, 20L);
  
        stream.forEach(System.out::println);
    }
}

输出 :

4
8
12
16
20