LongStream flatMap(LongFunction mapper)返回一个流,该流包括将流中的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容而得到的结果。这是一个中间操作。这些操作总是很懒。在Stream实例上调用中间操作,并在完成处理后将中间实例作为输出提供。
注意:将每个映射流的内容放入该流后,将其关闭。如果映射的流为null,则使用空流。
用法:
LongStream flatMap(LongFunction<? extends LongStream> mapper)
参数:
- LongStream:一系列原始long值元素。
- LongFunction:接受long值参数并产生结果的函数。
- 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
相关用法
- Java LongStream map(LongUnaryOperator mapper)用法及代码示例
- Java DoubleStream map(DoubleUnaryOperator mapper)用法及代码示例
- Java IntStream map(IntUnaryOperator mapper)用法及代码示例
- Java DoubleStream flatMap(DoubleFunction mapper)用法及代码示例
- Java IntStream flatMap(IntFunction mapper)用法及代码示例
- Java Collectors averagingLong(ToLongFunction mapper)用法及代码示例
- Java LongStream sum()用法及代码示例
- Java LongStream of()用法及代码示例
- Java LongStream min()用法及代码示例
- Java LongStream skip()用法及代码示例
- Java LongStream max()用法及代码示例
- Java LongStream asDoubleStream()用法及代码示例
- Java LongStream mapToDouble()用法及代码示例
- Java LongStream toArray()用法及代码示例
- Java LongStream iterator()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream flatMap(LongFunction mapper) in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。