LongStream map(LongUnaryOperator mapper)返回一個流,該流包含將給定函數應用於此流的元素的結果。這是一個中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
用法:
LongStream map(LongUnaryOperator mapper)
參數:
- LongStream:一係列原始Long-valued元素。
- LongUnaryOperator:對單個long值操作數進行的運算會產生long值結果。
- mapper:適用於每個元素的無狀態函數,該函數返回新流。
返回值:LongStream map(LongUnaryOperator mapper)通過應用給定函數返回流。
範例1:使用LongStream map()獲得LongStream元素平方的負數。
// Java code for LongStream map
// (LongUnaryOperator mapper) to get a
// stream by applying the given function.
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 map()
LongStream stream2 = stream1.map(num -> (-num * num));
// Displaying the resulting LongStream
stream2.forEach(System.out::println);
}
}
輸出:
-16 -25 -36 -49
範例2:使用LongStream map()獲取給定範圍內LongStream的一半元素。
// Java code for LongStream map
// (LongUnaryOperator mapper) to get a
// stream by applying the given function.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream in range [2, 7)
LongStream stream1 = LongStream.range(2, 7);
// Using LongStream map()
LongStream stream2 = stream1.map(num -> (num / 2));
// Displaying the resulting LongStream
stream2.forEach(System.out::println);
}
}
輸出:
1 1 2 2 3
相關用法
- Java LongStream flatMap(LongFunction 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 map(LongUnaryOperator mapper) in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。