- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the asymmetric difference of two sets in Java
Use removeAll() method to get the asymmetric difference of two sets.
First set −
HashSet <String> set1 = new HashSet <String>(); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");
Second set −
HashSet <String> set2 = new HashSet <String>(); set2.add("Mat");
To get the asymmetric difference −
set1.removeAll(set2);
The following is an example that displays how to get the asymmetric difference between two sets −
Example
import java.util.*; public class Demo { public static void main(String args[]) { HashSet <String> set1 = new HashSet <String>(); HashSet <String> set2 = new HashSet <String>(); set1.add("Mat"); set1.add("Sat"); set1.add("Cat"); System.out.println("Set1 = "+ set1); set2.add("Mat"); System.out.println("Set2 = "+ set2); set1.removeAll(set2); System.out.println("Asymmetric difference = "+ set1); } }
Output
Set1 = [Mat, Sat, Cat] Set2 = [Mat] Asymmetric difference = [Sat, Cat]
- Related Articles
- Get the intersection of two sets in Java
- Get the union of two sets in Java
- Java Program to Calculate the difference between two sets
- Merge two sets in Java
- Swift Program to Find the Symmetric Difference of Two Sets
- Java Program to Calculate the intersection of two sets
- Java Program to Calculate union of two sets
- Java Program to compare two sets
- Difference between Asymmetric and Symmetric Multiprocessing
- Difference Between Symmetric and Asymmetric Multiprocessing
- Difference between Asymmetric Information and Adverse Selection
- Cartesian product of two sets in JavaScript
- Finding union of two sets in JavaScript
- Possible two sets from first N natural numbers difference of sums as D in C++
- Adding two Sets in Javascript

Advertisements