-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQAonStream.java
More file actions
56 lines (40 loc) · 1.55 KB
/
Copy pathQAonStream.java
File metadata and controls
56 lines (40 loc) · 1.55 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 streamInterviewQA;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import data.Student;
import data.StudentDataBase;
/**
* 1. start "J" 2. upperCase 3. sort
*/
public class QAonStream {
static Predicate<Student> startWith = s -> s.getName().startsWith("J");
static Function<Student, String> mapperToUpperCaseReturnString = (s) -> {
return s.getName().toUpperCase();
};
static Function<Student, Student> mapperToUpperCaseReturnStudent = (student) -> {
String upperCaseName = student.getName().toUpperCase();
student.setName(upperCaseName);
return student;
};
static Comparator<String> sortCompString = Comparator.naturalOrder();
static Comparator<Student> sortCompStudent = Comparator.comparing(Student::getName);
public static void main(String[] args) {
onString();
onStudentObject();
}
private static void onStudentObject() {
List<Student> collect = StudentDataBase.getAllStudents().stream()
.filter(Objects::nonNull).filter(startWith).map(mapperToUpperCaseReturnStudent)
.sorted(sortCompStudent).collect(Collectors.toList());
System.out.println(collect);
}
public static void onString() {
List<String> collect = StudentDataBase.getAllStudents().stream().filter(Objects::nonNull).filter(startWith)
.map(mapperToUpperCaseReturnString).sorted(sortCompString).collect(Collectors.toList());
System.out.println(collect);
}
}