-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFP02StreamOperations.java
More file actions
40 lines (28 loc) · 1.11 KB
/
FP02StreamOperations.java
File metadata and controls
40 lines (28 loc) · 1.11 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
package programming;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class FP02StreamOperations {
@SuppressWarnings("unused")
public static void main(String[] args) {
List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15);
numbers.stream()
.distinct() //Stream<T> Intermediate
.sorted() //Stream<T>
.forEach(System.out::println); //void
List<Integer> squaredNumbers = numbers.stream()
.map(number -> number * number) //Stream<R>
.collect(Collectors.toList()); //R
List<Integer> evenNumbersOnly = numbers.stream()
.filter(x -> x % 2 == 0) //Stream<T>
.collect(Collectors.toList());
int sum = numbers.stream()
.reduce(0, (x,y) -> x*x + y*y); //T
int greatest = numbers.stream()
.reduce(Integer.MIN_VALUE, (x,y)-> x>y ? x:y);
List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes");
List<String> coursesSortedByLengthOfCourseTitle = courses.stream()
.sorted(Comparator.comparing(str -> str.length()))
.collect(Collectors.toList());
}
}