forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareListsWithHashSetEx.java
More file actions
44 lines (31 loc) · 1008 Bytes
/
CompareListsWithHashSetEx.java
File metadata and controls
44 lines (31 loc) · 1008 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
35
36
37
38
39
40
41
42
43
44
package com.zetcode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
// Compare lists by ignoring duplicates
public class CompareListsWithHashSetEx {
public static void main(String[] args) {
var words = new ArrayList<String>();
var words2 = new ArrayList<String>();
words.add("blue");
words.add("green");
words.add("red");
words.add("yellow");
words2.add("green");
words2.add("blue");
words2.add("red");
words2.add("yellow");
boolean equal = listEqualsIgnoreOrder(words, words2);
if (equal) {
System.out.println("The lists are equal");
} else {
System.out.println("The lists are not equal");
}
}
private static <T> boolean listEqualsIgnoreOrder(List<T> l1, List<T> l2) {
if (l1 == null || l2 == null) {
return l1 == l2;
}
return new HashSet<>(l1).equals(new HashSet<>(l2));
}
}