长流boxed()返回一个由该流的元素组成的Stream,每个元素都装箱成Long。
注意:LongStream boxed()是中间操作。这些操作总是很懒。在Stream实例上调用中间操作,并在完成处理后将中间实例作为输出提供。
用法:
Stream<Long> boxed()
参数:
- Stream : 支持顺序和并行聚合操作的一系列元素。
- Long : Long类将一个基本类型的值包装在一个对象中。 Long类型的对象包含一个long类型的字段。
返回值:该函数将装箱的流返回到Long。
示例1:
// Java code for LongStream boxed()
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.range(3L, 8L);
// Creating a Stream of Longs
// Using LongStream boxed() to return
// a Stream consisting of the elements
// of this stream, each boxed to a Long.
Stream<Long> stream1 = stream.boxed();
// Displaying the elements
stream1.forEach(System.out::println);
}
}
输出:
3 4 5 6 7
示例2:
// Java code for LongStream boxed()
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.range(3L, 8L);
// Creating a Stream of Longs
// Using LongStream boxed() to return
// a Stream consisting of the elements
// of this stream, each boxed to a Long.
Stream<Long> stream1 = stream.boxed();
Stream<Object> stream2 = Stream.concat(stream1,
Stream.of("Geeks", "for", "Geeks"));
// Displaying the elements
stream2.forEach(System.out::println);
}
}
输出:
3 4 5 6 7 Geeks for Geeks
相关用法
- Java IntStream boxed()用法及代码示例
- Java DoubleStream boxed()用法及代码示例
- Java LongStream of()用法及代码示例
- Java LongStream sum()用法及代码示例
- Java LongStream findFirst()用法及代码示例
- Java LongStream parallel()用法及代码示例
- Java LongStream sorted()用法及代码示例
- Java LongStream concat()用法及代码示例
- Java LongStream builder()用法及代码示例
- Java LongStream summaryStatistics()用法及代码示例
- Java LongStream skip()用法及代码示例
- Java LongStream sequential()用法及代码示例
- Java LongStream range()用法及代码示例
- Java LongStream rangeClosed()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream boxed() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。