LongStream forEach() method in Java

Java 8Object Oriented ProgrammingProgramming

<p>The forEach() method of the LongStream class in Java performs an action for each element of this stream.</p><p>The syntax is as follows</p><pre class="result notranslate">void forEach(LongConsumer action)</pre><p>Here, the parameter action is a non-interfering action to perform on the elements. LongConsumer represents an operation that accepts a single long-valued argument and returns no result.</p><p>To use the LongStream class in Java, import the following package</p><pre class="result notranslate">import java.util.stream.LongStream;</pre><p>The following is an example to implement LongStream forEach() method in Java</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/Xbsy1O" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">import java.util.*; import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(1000L, 12000L, 30000L); longStream.forEach(System.out::println); } }</pre><h2>Output</h2><pre class="result notranslate">1000 12000 30000</pre>
Updated on 30-Jul-2019 22:30:25