forked from dilipsundarraj1/java-8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsExample.java
More file actions
56 lines (42 loc) · 1.92 KB
/
Copy pathStreamsExample.java
File metadata and controls
56 lines (42 loc) · 1.92 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
package com.learnJava.streams;
import com.learnJava.data.Student;
import com.learnJava.data.StudentDataBase;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class StreamsExample {
public static void main(String[] args) {
Predicate<Student> gradePredicate = student -> student.getGradeLevel()>=3;
Predicate<Student> gpaPredicate = student -> student.getGradeLevel()>=3.9;
/*
List<String> names = Arrays.asList("adam","dan","jenny");
names.stream();
names.parallelStream();
StudentDataBase.getAllStudents().stream();
StudentDataBase.getAllStudents().parallelStream();
*/
Map<String,List<String>> studentMap = StudentDataBase.getAllStudents().stream(). //.parallelStream dont forger.
filter(gpaPredicate) // Stream<Student>
.collect(Collectors.toMap(Student::getName ,Student::getActivities ));
System.out.println("studentMap : " + studentMap);
List<String> studentActivities = StudentDataBase.getAllStudents().
stream() // Stream<Student>
.map(Student::getActivities) //<Stream<List<Activites>>
.flatMap(List::stream) //<Stream<String>
.distinct() // removes duplicates
.collect(Collectors.toList()); //collects it to a list.
List<String> namesList = StudentDataBase.getAllStudents().
stream() // Stream<Student>
.peek((student -> {
System.out.println(student);
}))
.map(Student::getName) //<Stream<List<Activites>>
.peek(System.out::println)
.distinct() // removes duplicates
.collect(Collectors.toList()); //collects it to a list.
System.out.println("namesList : " + namesList);
}
}