-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.java
More file actions
93 lines (71 loc) · 2.84 KB
/
ex1.java
File metadata and controls
93 lines (71 loc) · 2.84 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package e2021_normal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javafx.util.Pair;
/**
*
* @author hugo.ribeiro
*/
public class ex1 {
public Map<Integer, ArrayList<Pais>> numBoders(List<Pair<Pais,Pais>> lf) {
Map<Integer, ArrayList<Pais>> retorno = new TreeMap<>();
Map<Pais, Integer> mapaAux = new HashMap<>();
for(Pair<Pais,Pais> entry : lf) {
Pais key = entry.getKey();
Pais value = entry.getValue();
if(mapaAux.containsKey(key)) {
Integer auxValue = mapaAux.get(key);
mapaAux.put(key, auxValue+1);
} else {
mapaAux.put(key,new Integer(1));
}
if(mapaAux.containsKey(value)) {
Integer auxValue = mapaAux.get(value);
mapaAux.put(value, auxValue+1);
} else {
mapaAux.put(value, new Integer(1));
}
}
Iterator it = mapaAux.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
Pais retornoKey = (Pais) pair.getKey();
Integer retornoValue = (Integer) pair.getValue();
if(retorno.containsKey(retornoValue)) {
ArrayList<Pais> lista = retorno.get(retornoValue);
lista.add(retornoKey);
retorno.put(retornoValue,lista);
} else {
ArrayList<Pais> lista = new ArrayList<>();
lista.add(retornoKey);
retorno.put(retornoValue, lista);
}
}
return retorno;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Pair<Pais,Pais>> paises = new ArrayList<>();
ex1 instance = new ex1();
paises.add(new Pair<Pais,Pais>(new Pais("Portugal"), new Pais("Espanha")));
paises.add(new Pair<Pais,Pais>(new Pais("Espanha"), new Pais("França")));
paises.add(new Pair<Pais,Pais>(new Pais("Espanha"), new Pais("Gibraltar")));
paises.add(new Pair<Pais,Pais>(new Pais("Espanha"), new Pais("Andorra")));
paises.add(new Pair<Pais,Pais>(new Pais("França"), new Pais("Bélgica")));
paises.add(new Pair<Pais,Pais>(new Pais("França"), new Pais("Luxemburgo")));
Map<Integer, ArrayList<Pais>> mapa = instance.numBoders(paises);
System.out.println(mapa.toString());
}
}