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

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

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

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

句法 :

static  Stream of(T t)

Where, Stream is an interface and T
is the type of stream elements.
t is the single element and the
function returns a singleton 
sequential stream.

示例 1:单例字符串流。

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

输出 :

Geeks

示例 2:单例整数流。

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

输出 :

5

示例 3: Singleton Long 流。

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

输出 :

4