📜  Java中的DoubleStream flatMap(DoubleFunction mapper)

📅  最后修改于: 2022-05-13 01:54:31.575000             🧑  作者: Mango

Java中的DoubleStream flatMap(DoubleFunction mapper)

DoubleStream flatMap(DoubleFunction mapper)返回一个流,其中包含将此流的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容的结果。这是一个中间操作。这些操作总是懒惰的。在 Stream 实例上调用中间操作,在它们完成处理后,它们给出一个 Stream 实例作为输出。

注意:每个映射的流在其内容放入此流后关闭。如果映射流为空,则使用空流。

句法 :

DoubleStream flatMap(DoubleFunction mapper)

参数 :

  1. DoubleStream :原始双值元素的序列。
  2. DoubleFunction :接受双值参数并产生结果的函数。
  3. 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