Syntax error DoubleStream forEach() method in Java

DoubleStream forEach() method in Java



The forEach() method of the DoubleStream class performs an action for each element of this stream.

The syntax is as follows:

void forEach(DoubleConsumer action)

Here, DoubleConsumer represents an operation that accepts a single double-valued argument and returns no result. The parameter action is a non-interfering action to perform on the elements.

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

import java.util.stream.DoubleStream;

Create a DoubleStream and add some elements to the stream:

DoubleStream doubleStream = DoubleStream.of(45.7, 67.8, 89.7, 95.6);

Now, display the elements:

doubleStream.forEach(System.out::println);

The following is an example to implement DoubleStream forEach() method in Java:

Example

 Live Demo

import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args){
      DoubleStream doubleStream = DoubleStream.of(45.7, 67.8, 89.7, 95.6);
      doubleStream.forEach(System.out::println);
   }
}

Output

45.7
67.8
89.7
95.6
Updated on: 2019-07-30T22:30:25+05:30

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements