Java Stream of(T t)示例
Stream of(T t) 返回一个包含单个元素的顺序流,即一个单子顺序流。顺序流的工作就像使用单个核心的for-loop。另一方面,并行流将提供的任务分成多个,并在不同的线程中运行,利用计算机的多个核心。
语法:
static < T > Stream< T > of( T t)
其中,Stream是一个接口,T 是流元素的类型。T是单一元素,并且该 函数返回一个单子 顺序的流。
例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<String> 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<Integer> stream = Stream.of(5);
stream.forEach(System.out::println);
}
}
输出:
5
例3: 单子长流。
// 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<Long> stream = Stream.of(4L);
stream.forEach(System.out::println);
}
}
输出:
4