流动Java中的of(T)及其示例

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

null

语法:

static <T> Stream<T> 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<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
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享