Stream flatMapToDouble(Function mapper)返回一个DoubleStream,该DoubleStream包含将该流的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容而得到的结果。 Stream flatMapToDouble(Function mapper)是一个中间操作。这些操作总是很懒。在Stream实例上调用中间操作,并在完成处理后将中间实例作为输出提供。
注意:将每个映射流的内容放入该流后,将其关闭。如果映射的流为null,则使用空流。
用法:
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) Where, DoubleStream is a sequence of primitive double-valued elements and T is the type of stream elements. mapper is a stateless function which is applied to each element and the function returns the new stream.
示例1:flatMapToDouble()函数,具有将字符串解析为两倍的操作。
// Java code for Stream flatMapToDouble
// (Function mapper) to get an DoubleStream
// 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 a list of Strings
List<String> list = Arrays.asList("1.5", "2.7", "3",
"4", "5.6");
// Using Stream flatMapToDouble(Function mapper)
list.stream().flatMapToDouble(num
-> DoubleStream.of(Double.parseDouble(num)))
.forEach(System.out::println);
}
}
输出:
1.5 2.7 3.0 4.0 5.6
示例2:flatMapToDouble()函数,用于返回具有字符串长度的流。
// Java code for Stream flatMapToDouble
// (Function mapper) to get an DoubleStream
// 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 a List of Strings
List<String> list = Arrays.asList("Geeks", "GFG",
"GeeksforGeeks", "gfg");
// Using Stream flatMapToDouble(Function mapper)
// to get length of all strings present in list
list.stream().flatMapToDouble(str
-> DoubleStream.of(str.length()))
.forEach(System.out::println);
}
}
输出:
5.0 3.0 13.0 3.0
相关用法
- Java Stream map()用法及代码示例
- Java Stream peek()用法及代码示例
- Java Stream min()用法及代码示例
- Java Stream.max()用法及代码示例
- Java Stream flatMapToInt()用法及代码示例
- Java Stream findAny()用法及代码示例
- Java Stream filter()用法及代码示例
- Java Stream anyMatch()用法及代码示例
- Java Stream.reduce()用法及代码示例
- Java Stream allMatch()用法及代码示例
- Java Stream flatMapToLong()用法及代码示例
- Java Stream findFirst()用法及代码示例
- Java Stream toArray()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Stream flatMapToDouble() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。