LongStream map() method in Java

Java 8Object Oriented ProgrammingProgramming

The map() method of the LongStream class in Java returns a stream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows:

LongStream map(LongUnaryOperator mapper)

Here, the parameter mapper is a stateless function to apply to each element. The LongUnaryOperator represents an operation on a single long-valued operand that produces a long-valued result.

To use the LongStream class in Java, import the following package:

import java.util.stream.LongStream;

Create a LongStream and add some elements:

LongStream longStream1 = LongStream.of(15L, 30L, 45L, 67L, 80L);

Now, create another LongStream and map it to a condition set for the elements of longStream1:

LongStream longStream2 = longStream1.map(a → (a+a+a));

The following is an example to implement LongStream map() method in Java:

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream1 = LongStream.of(15L, 30L, 45L, 67L, 80L);
      LongStream longStream2 = longStream1.map(a → (a+a+a));
      longStream2.forEach(System.out::println);
   }
}

Here is the output:

45
90
135
201
240
raja
Published on 28-Mar-2019 16:01:19
Advertisements