-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathClasseComparator.java
45 lines (35 loc) · 1.08 KB
/
ClasseComparator.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
41
42
43
44
45
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class ClasseComparator {
public static void main(String[] args) {
Map<String, Pessoa> map = new TreeMap<>();
map.put("pessoa1", new Pessoa("Jubileu"));
map.put("pessoa2", new Pessoa("Pica Pau"));
map.put("pessoa3", new Pessoa("Zeca Urubu"));
System.out.println(map);
}
static class Pessoa implements Comparator<Pessoa> {
private static int sequencia = 1;
private Integer id;
private String nome;
public Pessoa(String nome) {
this.nome = nome;
this.id = sequencia++;
}
public Integer getId() {
return id;
}
public String getNome() {
return nome;
}
@Override
public int compare(ClasseComparator.Pessoa o1, ClasseComparator.Pessoa o2) {
return Integer.compare(o1.id, o2.id);
}
@Override
public String toString() {
return "Pessoa [id=" + id + ", nome=" + nome + "]";
}
}
}