LongStream flatMap(LongFunction mapper) 返回一个流,该流包含将该流的每个元素替换为通过应用提供的 映射函数 每一个元素。这是一个 中间操作 .这些操作总是懒惰的。在流实例上调用中间操作,完成处理后,它们将流实例作为输出。
null
注: 每个映射流在其内容被放入该流后关闭。如果映射流为null,则使用空流。
语法:
LongStream flatMap(LongFunction<? extends LongStream> mapper)
参数:
- 长流: 原始长值元素的序列。
- 长功能: 接受长值参数并产生结果的函数。
- 制图员: 应用于每个元素的无状态函数,该函数返回新流。
返回值: LongStream flatMap(LongFunction mapper)使用映射函数按映射流返回流。
例1: 使用LongStream flatMap()获取LongStream元素的立方体。
// Java code for LongStream flatMap // (LongFunction mapper) to get a stream // consisting of the results of replacing // each element of this stream with the // contents of a mapped stream import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream1 = LongStream.of(4L, 5L, 6L, 7L); // Using LongStream flatMap() LongStream stream2 = stream1.flatMap(num -> LongStream.of(num * num * num)); // Displaying the resulting LongStream stream2.forEach(System.out::println); } } |
输出:
64 125 216 343
例2: 使用LongStream flatMap()获取LongStream元素二进制表示形式中的设置位计数。
// Java code for LongStream flatMap // (LongFunction mapper) to get a stream // consisting of the results of replacing // each element of this stream with the // contents of a mapped stream import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream1 = LongStream.of(49L, 64L, 81L, 100L); // Using LongStream flatMap() LongStream stream2 = stream1.flatMap(num -> LongStream.of(Long.bitCount(num))); // Displaying the resulting LongStream stream2.forEach(System.out::println); } } |
输出:
3 1 3 3
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END