-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTesteMethodReference.java
More file actions
46 lines (37 loc) · 1.11 KB
/
Copy pathTesteMethodReference.java
File metadata and controls
46 lines (37 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
41
42
43
44
45
46
import static java.lang.System.out;
import java.util.*;
/**
* Exemplo de uso de Method References.
*
*/
public class TesteMethodReference {
public static void main(String args[]){
List<Aluno> alunos = buildAlunos();
Collections.sort(alunos, Aluno::compareByNome);
out.println("---------------\nAlunos da turma:");
for (Aluno a: alunos) {
out.println("\t" + a);
}
TesteMethodReference mr = new TesteMethodReference();
Collections.sort(alunos, mr::otherCompareByNome);
out.println("---------------\nAlunos da turma (reversa):");
for (Aluno a: alunos) {
out.println("\t" + a);
}
}
static List<Aluno> buildAlunos() {
List<Aluno> alunos = new ArrayList<Aluno>();
alunos.add(new Aluno("Claudia", 7));
alunos.add(new Aluno("Paula", 9));
alunos.add(new Aluno("Ricardo", 6));
alunos.add(new Aluno("Flavia", 4));
alunos.add(new Aluno("Mario", 8));
alunos.add(new Aluno("Ana", 5));
return alunos;
}
int otherCompareByNome(Aluno a1, Aluno a2) {
return a2.getNome().compareTo(a1.getNome());
}
}
//fazer o sort utilizando method reference static
//fazer o sort utilizando otherCompareByNome