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