LongStream map() method in Java

Java 8Object Oriented ProgrammingProgramming

<p>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.</p><p>The syntax is as follows:</p><pre class="prettyprint notranslate">LongStream map(LongUnaryOperator mapper)</pre><p>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.</p><p>To use the LongStream class in Java, import the following package:</p><pre class="prettyprint notranslate">import java.util.stream.LongStream;</pre><p>Create a LongStream and add some elements:</p><pre class="prettyprint notranslate">LongStream longStream1 = LongStream.of(15L, 30L, 45L, 67L, 80L);</pre><p>Now, create another LongStream and map it to a condition set for the elements of longStream1:</p><pre class="prettyprint notranslate">LongStream longStream2 = longStream1.map(a &rarr; (a+a+a));</pre><p>The following is an example to implement LongStream map() method in Java:</p><pre class="prettyprint notranslate">import java.util.stream.LongStream; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; LongStream longStream1 = LongStream.of(15L, 30L, 45L, 67L, 80L); &nbsp; &nbsp; &nbsp; LongStream longStream2 = longStream1.map(a &rarr; (a+a+a)); &nbsp; &nbsp; &nbsp; longStream2.forEach(System.out::println); &nbsp; &nbsp;} }</pre><p>Here is the output:</p><pre class="result notranslate">45 90 135 201 240</pre>
Updated on 30-Jul-2019 22:30:25