Difference and Symmetric Difference on Sets in Java
This post will discuss how to perform difference and symmetric difference operations on sets in Java.
The difference operation on two sets returns a set containing the elements of the first set that are not present in the second set. For example, if we have two sets A = {1, 2, 3, 4} and B = {2, 4, 6}, then the difference of A and B is {1, 3}, which are the elements of A that are not in B.
The symmetric difference of two sets is the set of elements that are contained in either set, but not in both. For example, if set A is {1, 2, 3} and set B is {2, 3, 4}, then the symmetric difference of A and B is {1, 4}.
There are several ways to perform difference and symmetric difference operations on sets in Java, besides using the third party libraries. Here are some of them:
1. Using Java Collections Framework
The Java Collections Framework provides removeAll() method that can be used to perform difference and symmetric difference operations on sets. The removeAll() method removes all elements from a set that are also contained in another collection. It can be used to perform the difference operation on sets by passing one set as the receiver and another set as the argument. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> set2 = new HashSet<>(Arrays.asList(2, 4)); Set<Integer> diff = new HashSet<>(set1); diff.removeAll(set2); System.out.println(diff); // [1, 3] } } |
You can perform the symmetric difference operation on sets by combining the difference and union operations. You can use the removeAll() method with the addAll() method, which adds all elements from another collection to a set. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> set2 = new HashSet<>(Arrays.asList(2, 4)); // Create copies of the original sets Set<Integer> diff1 = new HashSet<>(set1); Set<Integer> diff2 = new HashSet<>(set2); // Perform difference operation on both copies diff1.removeAll(set2); // {1, 3} diff2.removeAll(set1); // {4} // Perform union operation on the results diff1.addAll(diff2); System.out.println(diff1); // [1, 3, 4] } } |
However, these methods require multiple steps and intermediate objects to perform the difference and symmetric difference operations on sets. These methods are not efficient and eager. They create new set objects to store the intermediate results, which may consume more memory and computation time than necessary.
2. Using Stream API
The Java Stream API provides Stream.filter() method that can be used to perform difference and symmetric difference operations on sets. The filter() method returns a stream that consists of elements that match a given predicate. It can be used to perform the difference operation on sets by passing one set as the source of the stream and another set as the argument of the predicate. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; class Main { public static void main(String[] args) { Set<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> set2 = new HashSet<>(Arrays.asList(2, 4)); Set<Integer> diff = set1.stream() .filter(e -> !set2.contains(e)) .collect(Collectors.toSet()); System.out.println(diff); // [1, 3] } } |
You can perform the symmetric difference operation on sets by combining the difference and union operations. You can use the Stream.filter() method with the Stream.concat() method, which performs the concatenation of the two streams. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { Set<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3)); Set<Integer> set2 = new HashSet<>(Arrays.asList(2, 4)); // Perform difference operation on both sets Stream<Integer> diff1 = set1.stream().filter(e -> !set2.contains(e)); Stream<Integer> diff2 = set2.stream().filter(e -> !set1.contains(e)); // Perform union operation on the results Set<Integer> symDiff = Stream.concat(diff1, diff2) // {1, 3}, {4} .collect(Collectors.toSet()); System.out.println(symDiff); // [1, 3, 4] } } |
These methods are efficient and lazy. They do not create new set objects to store the result, but instead return a stream that is backed by the original sets. This saves memory and computation time, especially if the result is only used for terminal operations such as collect or count.
That’s all about performing difference and symmetric difference operations on sets in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)