-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.java
More file actions
76 lines (59 loc) · 2.03 KB
/
ex1.java
File metadata and controls
76 lines (59 loc) · 2.03 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
/*
* 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 e2020_normal;
import java.util.ArrayList;
import java.util.List;
import javafx.util.Pair;
/**
*
* @author hugo.ribeiro
*/
public class ex1 {
public static<K, E extends Comparable<E>> List<Pair<K,E>> mergeLists(List<Pair<K,E>> A, List<Pair<K,E>> B) {
List<Pair<K, E>> result = new ArrayList<>();
while(!A.isEmpty() && !B.isEmpty()) {
if(A.isEmpty()) {
result.add(B.get(0));
B.remove(0);
} else if(B.isEmpty()) {
result.add(A.get(0));
A.remove(0);
} else {
E valueA = A.get(0).getValue();
E valueB = B.get(0).getValue();
if(valueB.compareTo(valueA) > 0) {
result.add(A.get(0));
A.remove(0);
} else {
result.add(B.get(0));
B.remove(0);
}
}
}
return result;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
List<Pair<String,Integer>> A = new ArrayList<>();
A.add(new Pair<>("A",2));
A.add(new Pair<>("A",2));
A.add(new Pair<>("A",5));
List<Pair<String,Integer>> B = new ArrayList<>();
B.add(new Pair<>("B",1));
B.add(new Pair<>("B",1));
B.add(new Pair<>("B",2));
B.add(new Pair<>("B",3));
B.add(new Pair<>("B",4));
B.add(new Pair<>("B",4));
B.add(new Pair<>("B",5));
System.out.println("List A: "+A);
System.out.println("List B: "+B);
List<Pair<String,Integer>> result = mergeLists(A, B);
System.out.println("Resultado: "+result);
}
}