-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit2ex.java
More file actions
73 lines (59 loc) · 1.81 KB
/
Copy pathUnit2ex.java
File metadata and controls
73 lines (59 loc) · 1.81 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
package lambda_expression.unit1;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Unit2ex {
public static void main(String[] args) {
List<Person> people=Arrays.asList(
new Person("Charles","Dickens",60),
new Person("Lewis","Carroll",42),
new Person("Thomas","Carlyle",51),
new Person("charlotte","Bronte",45),
new Person("Matthew","Arnold",39)
);
//Steps 1: Sort list by last name
Collections.sort(people,new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getLastName().compareTo(o2.getLastName());
}
});
//Steps 2: Create a method that prints all elements in the list
System.out.println("Printing all persons");
printAll(people);
//Steps 3: Create a method that prints all people that have last name beginning with c
System.out.println("Printing all persons with first name beginning with C");
printConditionally(people,new Condition() {
@Override
public boolean test(Person p) {
return p.getFirstName().startsWith("C");
}
});
System.out.println("Printing all persons with last name beginning with C");
printConditionally(people,new Condition() {
@Override
public boolean test(Person p) {
return p.getLastName().startsWith("C");
}
});
}
private static void printConditionally(List<Person> people,Condition condtion) {
for(Person p : people){
/*if(p.getLastName().startsWith("C")){
System.out.println(p);
}*/
if(condtion.test(p)){
System.out.println(p);
}
}
}
private static void printAll(List<Person> people) {
for(Person p: people){
System.out.println(p);
}
}
}
interface Condition{
boolean test(Person p);
}