A IntStream method iterator used to convert an iterator of this stream object. It is a terminal operation that returns an iterator for the elements of this stream.
1 PrimitiveIterator.OfInt iterator()
return : It returns an iterator object that includes an elements of this stream.
1 package com.java;
2
3 import java.util.Iterator;
4 import java.util.stream.IntStream;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 IntStream stream1 = IntStream.of(10, 20, 30, 40, 50);
11
12 Iterator<Integer> iterator = stream1.iterator();
13
14 System.out.println("Print stream elements using iterator !");
15 while(iterator.hasNext()) {
16 System.out.println(iterator.next());
17 }
18 }
19 }
In the above example, a stream object created using of() method. A stream method iterator() called that returns an iterator that includes an element of this stream. An element is iterated using while loop using hasNext() and next() method of iterator.
1 Print stream elements using iterator !
2 10
3 20
4 30
5 40
6 50
Iterator with intermediate operation
Iterator with intermediate operation
1 package com.java;
2
3 import java.util.Iterator;
4 import java.util.stream.IntStream;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 IntStream stream1 = IntStream.of(1, 2, 3, 4, 5);
11
12 Iterator<Integer> iterator = stream1
13
14 .map(ele -> ele * ele)
15 .iterator();
16
17 System.out.println("Print mapped elements using iterator !");
18 while(iterator.hasNext()) {
19 System.out.println(iterator.next());
20 }
21 }
22 }
In the above example, a stream object created using of() method and a stream map() method called that maps to square of element and returns a resulting stream. A stream method iterator() called that returns an iterator of stream elements. A values are iterated using while loop using hasNext() and next() method.
1 Print mapped elements using iterator !
2 1
3 4
4 9
5 16
6 25
iterator() with empty stream
iterator() with empty stream
1 package com.java;
2
3 import java.util.Iterator;
4 import java.util.stream.IntStream;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 IntStream stream1 = IntStream.of();
11
12 Iterator<Integer> itr1 = stream1.iterator();
13 System.out.println("iterator() with empty stream !");
14 itr1.forEachRemaining(System.out::println);
15
16
17 IntStream stream2 = IntStream.of(12, 34, 23, 54, 67, 24);
18 Iterator<Integer> itr2 = stream2
19
20 .filter(ele -> ele % 7 == 0)
21 .iterator();
22 System.out.println("iterator() elements after mod 7 !");
23 itr2.forEachRemaining(System.out::println);
24 }
25 }
In the above example, an empty stream is created and call stream iterator() 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 7. A resulting stream does not contain an element and print message only.
1 iterator() with empty stream !
2 iterator() elements after mod 7 !
Related options for your search