-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPares.java
More file actions
34 lines (23 loc) · 780 Bytes
/
Pares.java
File metadata and controls
34 lines (23 loc) · 780 Bytes
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
package generics;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
public class Pares<C extends Number, V> {
private final Set<Par<C, V>> itens = new HashSet<>();
public void adicionar(C chave, V valor) {
if(chave == null) return;
Par<C, V> novoPar = new Par<C, V>(valor, chave);
if(itens.contains(novoPar)) {
itens.remove(novoPar);
}
itens.add(novoPar);
}
public V getValor(C chave) {
if(chave == null) return null;
Optional<Par<C, V>> parOpcional = itens.stream()
.filter(par -> chave.equals(par.getChave()))
.findFirst();
return parOpcional.isPresent()
? parOpcional.get().getValor() : null;
}
}