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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。