LongStream map(LongUnaryOperator mapper) in Java
LongStream map(LongUnaryOperator mapper) returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.
Syntax :
LongStream map(LongUnaryOperator mapper)
Parameters :
- LongStream : A sequence of primitive Long-valued elements.
- LongUnaryOperator : An operation on a single long-valued operand that produces an long-valued result.
- mapper : A stateless function which is applied to each element and the function returns the new stream.
Return Value : LongStream map(LongUnaryOperator mapper) returns a stream by applying the given function.
Example 1 : Using LongStream map() to get the negative of square of elements of LongStream.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream1 = LongStream.of(4L, 5L, 6L, 7L);
LongStream stream2 = stream1.map(num -> (-num * num));
stream2.forEach(System.out::println);
}
}
|
Example 2 :Using LongStream map() to get the half of elements of LongStream in given range.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream1 = LongStream.range( 2 , 7 );
LongStream stream2 = stream1.map(num -> (num / 2 ));
stream2.forEach(System.out::println);
}
}
|
Last Updated :
06 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...