forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareListsEx4.java
More file actions
77 lines (55 loc) · 2.05 KB
/
CompareListsEx4.java
File metadata and controls
77 lines (55 loc) · 2.05 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
package com.zetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Compare lists by traversing lists with for loops
public class CompareListsEx4 {
public static void main(String[] args) {
// Size & values of elements are same but only order is different
var vals1 = new ArrayList<>(Arrays.asList(65L, 74L, 89L));
var vals2 = new ArrayList<>(Arrays.asList(65L, 89L, 74L));
System.out.printf("Output: %s%n", compareLists(vals1, vals2));
// Values are different
vals1 = new ArrayList<>(Arrays.asList(6L, 7L, 89L));
vals2 = new ArrayList<>(Arrays.asList(6L, 89L, 74L));
System.out.printf("Output: %s%n", compareLists(vals1, vals2));
// Size is different
vals1 = new ArrayList<>(Arrays.asList(6L, 7L, 89L));
vals2 = new ArrayList<>(Arrays.asList(6L, 7L, 89L, 7L));
System.out.printf("Output: %s%n", compareLists(vals1, vals2));
// Both lists are null
vals1 = null;
vals2 = null;
System.out.printf("Output: %s%n", compareLists(vals1, vals2));
// Both lists are empty
vals1 = new ArrayList<>();
vals2 = new ArrayList<>();
System.out.printf("Output: %s%n", compareLists(vals1, vals2));
}
public static boolean compareLists(List<Long> l1, List<Long> l2) {
if (l1 == null && l2 == null) {
return true;
}
if (l1 != null && l2 != null) {
if (l1.size() == l2.size()) {
for (Long val1 : l1) {
boolean isEqual = false;
for (Long val2 : l2) {
if (val1.equals(val2)) {
isEqual = true;
break;
}
}
if (!isEqual) {
return false;
}
}
} else {
return false;
}
} else {
return false;
}
return true;
}
}