
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Compare Two Sets
The given task is to write Java programs that compare two sets and check if they are equal or not. Here, Set is an interface of the Java Collection Framework that extends the Collection interface and stores unique elements. Set depicts the features of a mathematical set. Hence, it allows all those operations that we can perform on a mathematical set, such as union, comparison, intersection, etc.
Since Set is an interface, we can't use its functionalities directly. For this purpose, we need a TreeSet class that implements the Set Interface and can access its methods.
How to Compare Two Sets in Java?
For the comparison operation of two sets, we can use a built-in method named equals(). It is used to check whether two given sets contain the same number and type of objects in the same order or not. It returns true if both sets satisfy the condition; otherwise false.
Let's see how we can use this method in our Java programs.
Example 1
In the following Java program, we will create two TreeSet classes with the same elements but in a different order. Despite this, when we compare them using the equals() method, it will return true because by default, TreeSet stores its elements in the sorted order.
import java.util.*; public class Example1 { public static void main(String args[]) { // Creating the first tree set TreeSet<String> treeSt1 = new TreeSet<>(); // Adding elements in tree set treeSt1.add("Tutorix"); treeSt1.add("Simply"); treeSt1.add("Easy"); treeSt1.add("Learning"); treeSt1.add("Tutorials"); treeSt1.add("Point"); System.out.println("Elements of the first set: " + treeSt1); // Creating the second tree set TreeSet<String> treeSt2 = new TreeSet<>(); // Adding elements in tree set treeSt2.add("Tutorials"); treeSt2.add("Point"); treeSt2.add("Tutorix"); treeSt2.add("Simply"); treeSt2.add("Easy"); treeSt2.add("Learning"); System.out.println("Elements of the second set: " + treeSt2); // comparing both sets if (treeSt1.equals(treeSt2)) { System.out.println("Both sets are equal"); } else { System.out.println("Both sets are not equal"); } } }
Output
Elements of the first set: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Elements of the second set: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Both sets are equal
Example 2
This is another Java program that illustrates how we can compare two sets using the equals() method. We will first initialize two arrays with identical elements in the same order and then convert them to sets using the asList() method so that we can compare them using the equals() method.
import java.util.*; public class Example2 { public static void main(String[] args) { // defining the first array String arrOne[] = { "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y" }; // creating an instance of TreeSet and storing the values of first array TreeSet<String> trSet1 = new TreeSet<String>(Arrays.asList(arrOne)); System.out.println("Elements of the first set: " + trSet1); // defining the second array String arrTwo[] = { "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y" }; // creating an instance of TreeSet and storing the values of second array TreeSet<String> trSet2 = new TreeSet<String>(Arrays.asList(arrTwo)); System.out.println("Elements of the second set: " + trSet2); // comparing both sets if (trSet1.equals(trSet2)) { System.out.println("Both sets are equal"); } else { System.out.println("Both sets are not equal"); } } }
Output
Elements of the first set: [P, Q, R, S, T, U, V, W, X, Y] Elements of the second set: [P, Q, R, S, T, U, V, W, X, Y] Both sets are equal
Conclusion
We started this article by defining the Set Interface of Java Collection Framework and in the next section, we have written two Java programs to compare and check whether two given sets are equal or not. For this purpose, we have used the TreeSet class and the equals() method of Set Interface.