LongStream iterator() method in Java

Java 8Object Oriented ProgrammingProgramming

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

The syntax is as follows.

PrimitiveIterator.OfLong iterator()

Here, PrimitiveIterator is an Iterator specialized for long values.

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

import java.util.stream.LongStream;

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

Example

 Live Demo

import java.util.*;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(15000L, 17000L, 25000L);
      PrimitiveIterator.OfLong i = longStream.iterator();
      while (i.hasNext()) {
         System.out.println(i.nextLong());
      }
   }
}

Output

15000
17000
25000
raja
Published on 29-Mar-2019 11:44:02
Advertisements