Skip to content

Commit 58eab2a

Browse files
Java 8 – How to convert Iterator to Stream
1 parent bf82bfb commit 58eab2a

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.javaprogramto.java8.streams.conversions;
2+
3+
import com.javaprogramto.models.Employee;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
import java.util.stream.StreamSupport;
9+
10+
public class IteratorToStream {
11+
12+
public static void main(String[] args) {
13+
14+
List<Employee> list = new ArrayList<>();
15+
16+
list.add(new Employee(100, "Santa Clara", 30));
17+
list.add(new Employee(101, "Sameera Bell", 25));
18+
list.add(new Employee(102, "Jhon Cena", 33));
19+
list.add(new Employee(103, "Tubel Nohn", 35));
20+
list.add(new Employee(104, "Meen Joseph", 31));
21+
22+
System.out.println("original List : " + list);
23+
24+
/*
25+
List to Iterator
26+
*/
27+
Iterator<Employee> it = list.iterator();
28+
29+
/*
30+
Iterator to Spliterator
31+
*/
32+
Spliterator<Employee> employeeSpliterator = Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED);
33+
34+
/*
35+
Spliterator to Stream
36+
*/
37+
Stream<Employee> employeeStream = StreamSupport.stream(employeeSpliterator, false);
38+
39+
40+
List<Employee> ageGt30List = employeeStream.peek(e -> System.out.println(e)).filter(e -> e.getAge() > 30).collect(Collectors.toList());
41+
42+
System.out.println("Employee age > 30 size: " + ageGt30List.size());
43+
System.out.println("Employee age > 30 : " + ageGt30List);
44+
45+
46+
}
47+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.javaprogramto.java8.streams.conversions;
2+
3+
import com.javaprogramto.models.Employee;
4+
5+
import java.util.*;
6+
import java.util.stream.Stream;
7+
import java.util.stream.StreamSupport;
8+
9+
public class IteratorToStreamUtility {
10+
11+
public static void main(String[] args) {
12+
13+
List<Employee> list = new ArrayList<>();
14+
15+
list.add(new Employee(200, "Momanth Soraga", 30));
16+
list.add(new Employee(201, "Fidha Lady", 25));
17+
18+
Stream<Employee> empStream = convertIteratorToStream(list);
19+
20+
21+
System.out.println("Employee Stream Count : " + empStream.count());
22+
23+
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
24+
25+
Stream<Integer> intStream = convertIteratorToStream(intList);
26+
27+
System.out.println("Integer Stream Count : " + intStream.count());
28+
29+
List<String> countiresList = Arrays.asList("USA", "India","UK","AUS");
30+
31+
Stream<String> countiresStream = convertIteratorToStream(countiresList);
32+
33+
System.out.println("String countries Stream Count : " + countiresStream.count());
34+
35+
36+
37+
}
38+
39+
/**
40+
* Generic method to take any value List type and return a generic Stream.
41+
* <p>
42+
* Main goal to convert Iterator to Stream.
43+
*
44+
* @param source
45+
* @param <T>
46+
* @return
47+
*/
48+
public static <T> Stream<T> convertIteratorToStream(List<T> source) {
49+
50+
/*
51+
List to Iterator
52+
*/
53+
Iterator<T> it = source.iterator();
54+
55+
/*
56+
Iterator to Spliterator
57+
*/
58+
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED);
59+
60+
/*
61+
Spliterator to Stream
62+
*/
63+
Stream<T> oututStream = StreamSupport.stream(spliterator, false);
64+
65+
return oututStream;
66+
}
67+
}

0 commit comments

Comments
 (0)