- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Java Program to compare two sets
The Java Collection Framework provides an interface named Set that extends the Collection interface and serves to store unique elements. It 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 and so forth. The agenda of this article is to write Java programs to copare two sets. For the comparison operation of two sets, Java provides a built-in method 'equals()' that we will discuss in the next section.
Java Program to Compare two Sets
We are going to use the following class and method in our Java program to compare two sets −
TreeSet Class
Since Set is an interface, we can't use its functionalities directly. For this purpose, we need TeeSet class that implements the Set Interface which means it can access all the methods of Set. It stores all the elements in the form tree structure and like Set, it does not allow the storage of duplicate elements.
equals() method
It is a method of Set which 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.
Syntax
setOne.equals(setTwo);
Now, let's jump into the Java programs to compare and check whether two given sets are equal or not.
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 which illustrates how we can compare two sets using the equals() method. We will first initialize two arrays and then, convert them to sets using asList() method so that we can compare them using 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.