forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareListsEx.java
More file actions
56 lines (40 loc) · 1.53 KB
/
CompareListsEx.java
File metadata and controls
56 lines (40 loc) · 1.53 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
package com.zetcode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
//https://stackoverflow.com/questions/1075656/simple-way-to-find-if-two-different-lists-contain-exactly-the-same-elements?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
public class CompareListsEx {
public static void main(String[] args) {
// Solution one
var words = List.of("dog", "pen", "sky", "rock");
var words2 = List.of("dog", "pen", "sky", "rock");
// O(n^2)
boolean equal = (words.size() == words2.size()) && words.containsAll(words2);
if (equal) {
System.out.println("Lists are equal");
} else {
System.out.println("Lists are not equal");
}
// Solution two
var listOne = new ArrayList<>(List.of("dog", "pen", "sky", "rock"));
var listTwo = new ArrayList<>(List.of("dog", "pen", "sky", "rock"));
listOne.retainAll(listTwo);
System.out.println(listOne);
// Solution three
var words3 = new ArrayList<String>();
var words4 = new ArrayList<String>();
words3.add("blue");
words3.add("green");
words3.add("red");
words3.add("yellow");
words4.add("blue");
words4.add("green");
words4.add("red");
words4.add("yellow");
if (Objects.equals(words3, words4)) {
System.out.println("Lists are equal");
} else {
System.out.println("Lists are not equal");
}
}
}