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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。