DoubleStream method spliterator() in Java
A DoubleStream method spliterator used to convert an spliterator of this stream object. It is a terminal operation that returns an spliterator for the elements of this stream.
Syntax
 1 Spliterator.OfDouble spliterator()
return : It returns an spliterator object that includes an elements of this stream.
DoubleStream method spliterator()
 1 package com.java;
 2 
 3 import java.util.Spliterator;
 4 import java.util.stream.DoubleStream;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         DoubleStream stream1 = DoubleStream.of(1.2, 1.9, 2.8, 4.9, 8.1);
 11         // convert stream to spliterator
 12         Spliterator<Double> iterator = stream1.spliterator();
 13 
 14         System.out.println("Print elements using spliterator !");
 15         iterator.forEachRemaining(System.out::println);
 16     }
 17 }
In the above example, a stream object created using of() method. A stream method spliterator() called that returns a spliterator that includes an element of this stream. An elements are iterating using forEachRemaining().
Output
 1 Print elements using spliterator !
 2 1.2
 3 1.9
 4 2.8
 5 4.9
 6 8.1

spliterator() with intermediate operation

spliterator() with intermediate operation
 1 package com.java;
 2 
 3 import java.util.Spliterator;
 4 import java.util.stream.DoubleStream;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         DoubleStream stream1 = DoubleStream.of(1.2, 1.9, 2.8, 4.9, 8.1);
 11         // convert stream to spliterator
 12         Spliterator<Double> iterator = stream1
 13                 /* map each element to square */
 14                 .map(ele -> ele * 2)
 15                 .spliterator();
 16 
 17         System.out.println("Print elements using spliterator !");
 18         iterator.forEachRemaining(System.out::println);
 19     }
 20 }
In the above example, a stream object created using of() method and a stream map() method called that maps element by multiplying with 2 and returns a resulting stream. A stream method spliterator() called that returns an spliterator of stream elements. An elements are iterating using forEachRemaining()
Output
 1 Print elements using spliterator !
 2 2.4
 3 3.8
 4 5.6
 5 9.8
 6 16.2

iterator() with empty stream

iterator() with empty stream
 1 package com.java;
 2 
 3 import java.util.Spliterator;
 4 import java.util.stream.DoubleStream;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         DoubleStream stream1 = DoubleStream.of();
 11         // spliterator() with empty stream
 12         Spliterator<Double> itr1 = stream1.spliterator();
 13         System.out.println("spliterator() with empty stream !");
 14         itr1.forEachRemaining(System.out::println);
 15 
 16         // spliterator() with empty stream, after filtering elements
 17         DoubleStream stream2 = DoubleStream.of(1.2, 1.9, 2.8, 4.9, 8.1);
 18         Spliterator<Double> itr2 = stream2
 19                 /* filter elements which are divisible by 1.5 */
 20                 .filter(ele -> ele % 1.5 == 0)
 21                 .spliterator();
 22         System.out.println("spliterator() elements after mod 1.5 !");
 23         itr2.forEachRemaining(System.out::println);
 24     }
 25 }
In the above example, an empty stream is created and call stream spliterator() method that returns an empty stream. In the second example, a stream object is created and filter() method called that filter out all elements as none of the element value is divisible by 1.5. A resulting stream does not contain an element and print message only.
Output
 1 spliterator() with empty stream !
 2 spliterator() elements after mod 1.5 !
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us