forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareListsEx2.java
More file actions
46 lines (32 loc) · 1.02 KB
/
CompareListsEx2.java
File metadata and controls
46 lines (32 loc) · 1.02 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
package com.zetcode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
// Compare lists by sorting and transferring to strings
// We assume that in-place sorting is OK
public class CompareListsEx2 {
public static boolean compareList(List<String> l1, List<String> l2) {
return l1.toString().contentEquals(l2.toString());
}
public static void main(String[] args) {
var one = new ArrayList<String>();
var two = new ArrayList<String>();
one.add("dog");
one.add("pen");
one.add("sky");
one.add("rock");
two.add("dog");
two.add("pen");
two.add("rock");
two.add("sky");
one.sort(Comparator.naturalOrder());
two.sort(Comparator.naturalOrder());
System.out.println(one);
System.out.println(two);
if (compareList(one, two)) {
System.out.println("The lists are equal");
} else {
System.out.println("The lists are not equal");
}
}
}