-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicStreamExample.java
More file actions
152 lines (113 loc) · 5.13 KB
/
BasicStreamExample.java
File metadata and controls
152 lines (113 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class BasicStreamExample {
public static void main(String[] args) {
// From a list
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
Stream<String> namesStream = names.stream();
// From an array
String[] cities = {"London", "Paris", "Tokyo"};
Stream<String> citiesStream = Arrays.stream(cities);
// From individual elements
Stream<String> streamOfNames = Stream.of("John", "Jane", "Jack");
}
// Filtering
//Filter elements based on a condition using filter.
public class SimpleStreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Filter even numbers
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println("Even Numbers: " + evenNumbers);
}
}
// Mapping
//Transform each element using map.
public class MappingExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("alice", "bob", "charlie");
// Convert each name to uppercase
List<String> uppercasedNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("Uppercased Names: " + uppercasedNames);
}
}
/*
* Intermediate and Terminal Operations
Intermediate Operations: Operations like
filter, map, sorted, etc., return a new
stream and are lazy (they don’t execute
until a terminal operation is called).
Terminal Operations: Operations like
collect, forEach, reduce finalize the
stream processing.
*/
public class SortingExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Banana", "Apple", "Orange", "Mango");
// Sort fruits alphabetically and collect as a list
List<String> sortedFruits = fruits.stream()
.sorted()
.collect(Collectors.toList());
System.out.println("Sorted Fruits: " + sortedFruits);
}
}
// Advanced Stream Operations
// Reducing
//The reduce method combines all elements into a single result using a binary operation.
public class ReducingExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Sum all numbers
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);
System.out.println("Sum of Numbers: " + sum);
}
}
// Using flatMap for Complex Data Structures
//The flatMap method is used to flatten a stream of collections into a single stream.
public class FlatMapExample {
public static void main(String[] args) {
List<List<String>> namesList = Arrays.asList(
Arrays.asList("Alice", "Bob"),
Arrays.asList("Charlie", "David"),
Arrays.asList("Eve", "Frank")
);
// Flatten the nested list
List<String> flattenedList = namesList.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
System.out.println("Flattened List: " + flattenedList);
}
}
// Parallel Streams for Concurrency
// Using parallelStream can leverage multiple cores for faster processing.
public class ParallelStreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Using parallel stream to find squares of numbers
numbers.parallelStream()
.map(n -> n * n)
.forEach(System.out::println);
}
}
//Combining Multiple Stream Operations
//An example showing a more advanced use case with multiple operations:
public class AdvancedStreamExample {
public static void main(String[] args) {
List<String> words = Arrays.asList("Stream", "Java", "Parallel", "Filter", "Lambda");
// Find words with length > 5, convert them to uppercase, sort them, and collect as a list
List<String> result = words.stream()
.filter(word -> word.length() > 5)
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
System.out.println("Filtered and Processed Words: " + result);
}
}
}