liushuai’s note


title:Java

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
import java.util.ArrayList;
import java.util.List;

public class {

private List<Person> list = new ArrayList<>();

public List<Person> findByName(String name) {
return find(new Criteria() {

public boolean matches(Person person) {
return name.equals(person.getName());
}
});
}

public List<Person> findByGender(String gender) {
return find(new Criteria() {

public boolean matches(Person person) {
return gender.equals(person.getGender());
}
});
}

public List<Person> find(Criteria criteria){
List<Person> people = new ArrayList<>();
for (Person p : list){
if(criteria.matches(p)){
people.add(p);
}
}
return people;
}

}

interface Criteria{
boolean matches(Person person);
}