DoubleStream iterator() method in Java

Java 8Object Oriented ProgrammingProgramming

The iterator() method of the DoubleStream class in Java returns an iterator for the elements of this stream.

The syntax is as follows

PrimitiveIterator.OfDouble iterator()

Here, PrimitiveIterator.OfDouble is an Iterator specialized for double values.

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

import java.util.stream.DoubleStream;

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

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(50.6, 69.8, 81.8, 95.6, 106.9);
      PrimitiveIterator.OfDouble res = doubleStream.iterator();
      while (res.hasNext()) {
         System.out.println(res.nextDouble());
      }
   }
}

Output

50.6
69.8
81.8
95.6
106.9
raja
Published on 28-Mar-2019 07:56:53
Advertisements