DoubleStream flatMap(DoubleFunction mapper)返回一个流,该流包括将流中的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容而得到的结果。这是一个中间操作。这些操作总是很懒。在Stream实例上调用中间操作,并在完成处理后将中间实例作为输出提供。
注意:将每个映射流的内容放入该流后,将其关闭。如果映射的流为null,则使用空流。
用法:
DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)
参数:
- DoubleStream:一系列原始双值元素。
- DoubleFunction:接受双值参数并产生结果的函数。
- mapper:适用于每个元素的无状态函数,该函数返回新流。
返回值:DoubleStream flatMap(DoubleFunction mapper)使用映射函数通过映射流返回一个流。
范例1:使用DoubleStream flatMap()获得DoubleStream元素的多维数据集。
// Java code for DoubleStream flatMap
// (DoubleFunction 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.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an DoubleStream
DoubleStream stream1 = DoubleStream.of(4.2, 5.3, 6.6, 7.0);
// Using DoubleStream flatMap()
DoubleStream stream2 = stream1.flatMap(num
-> DoubleStream.of(num * num * num));
// Displaying the resulting DoubleStream
stream2.forEach(System.out::println);
}
}
输出:
74.08800000000001 148.87699999999998 287.496 343.0
范例2:使用DoubleStream flatMap()将DoubleStream的每个元素乘以0.7
// Java code for DoubleStream flatMap
// (DoubleFunction 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.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an DoubleStream
DoubleStream stream1 = DoubleStream.of(5.2, 6.4, 8.1, 10.0);
// Using DoubleStream flatMap()
DoubleStream stream2 = stream1.flatMap(num
-> DoubleStream.of(num * 0.7));
// Displaying the resulting IntStream
stream2.forEach(System.out::println);
}
}
输出:
3.6399999999999997 4.4799999999999995 5.669999999999999 7.0
相关用法
- Java DoubleStream map(DoubleUnaryOperator mapper)用法及代码示例
- Java IntStream map(IntUnaryOperator mapper)用法及代码示例
- Java LongStream map(LongUnaryOperator mapper)用法及代码示例
- Java LongStream flatMap(LongFunction mapper)用法及代码示例
- Java IntStream flatMap(IntFunction mapper)用法及代码示例
- Java Collectors averagingLong(ToLongFunction mapper)用法及代码示例
- Java DoubleStream sum()用法及代码示例
- Java DoubleStream of()用法及代码示例
- Java DoubleStream mapToObj()用法及代码示例
- Java DoubleStream min()用法及代码示例
- Java DoubleStream sequential()用法及代码示例
- Java DoubleStream max()用法及代码示例
- Java DoubleStream iterator()用法及代码示例
- Java DoubleStream skip()用法及代码示例
- Java DoubleStream toArray()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 DoubleStream flatMap(DoubleFunction mapper) in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。