Skip to content

Commit 9d72883

Browse files
committed
aph-15311 difference between two Sets
1 parent 970b341 commit 9d72883

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ This project contains Core Java code samples that are used as examples on [amitp
119119

120120
### [Comparing Two Lists In Java](https://www.amitph.com/java-compare-lists/)
121121

122+
###[Finding the Difference Between Two Java Sets](https://www.amitph.com/java-find-sets-difference/)
123+
122124

123125
## How to use
124126
*Clone the project using*

java-collections/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ This project contains Java Collections code samples that are used as examples on
8282

8383
### [Comparing Two Lists In Java](https://www.amitph.com/java-compare-lists/)
8484

85+
###[Finding the Difference Between Two Java Sets](https://www.amitph.com/java-find-sets-difference/)
86+
87+
8588
## How to use
8689
*Clone the project using*
8790
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.amitph.java.collections.set;
2+
3+
import com.google.common.collect.Sets;
4+
import org.apache.commons.collections4.CollectionUtils;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
9+
10+
public class SetsDifferenceFinder {
11+
private final Set<String> one = Set.of("Ray", "Tim", "Leo", "Bee");
12+
private final Set<String> two = Set.of("Ray", "Mia", "Lyn", "Bee", "Zoe");
13+
14+
public void usingPlainJava() {
15+
Set<String> difference = new HashSet<>(one);
16+
difference.removeAll(two);
17+
18+
System.out.println(difference);
19+
}
20+
21+
public void usingStreams() {
22+
Set<String> difference = one.stream()
23+
.filter(e -> !two.contains(e))
24+
.collect(Collectors.toSet());
25+
System.out.println(difference);
26+
}
27+
28+
public void usingApacheCommons() {
29+
Set<String> differences = new HashSet<>(CollectionUtils.removeAll(one, two));
30+
System.out.println(differences);
31+
}
32+
33+
public void usingGuava() {
34+
Set<String> differences = Sets.difference(one, two);
35+
System.out.println(differences);
36+
}
37+
38+
public void symmetricDifferenceUsingApacheCommons(){
39+
Set<String> differences = new HashSet<>(CollectionUtils.disjunction(one, two));
40+
System.out.println(differences);
41+
}
42+
43+
public static void main(String[] a) {
44+
SetsDifferenceFinder differenceFinder = new SetsDifferenceFinder();
45+
46+
System.out.println("Plain Java:");
47+
differenceFinder.usingPlainJava();
48+
49+
System.out.println();
50+
System.out.println("Java Streams:");
51+
differenceFinder.usingStreams();
52+
53+
System.out.println();
54+
System.out.println("Apache Commons Collection:");
55+
differenceFinder.usingApacheCommons();
56+
57+
System.out.println();
58+
System.out.println("Guava:");
59+
differenceFinder.usingGuava();
60+
61+
System.out.println();
62+
System.out.println("Symmetric Difference with Apache Commons Collection:");
63+
differenceFinder.symmetricDifferenceUsingApacheCommons();
64+
}
65+
}

0 commit comments

Comments
 (0)