Home >Java >javaTutorial >Java program to compare two collections
Java Collection Framework provides an interface called Set, which extends the Collection interface and is used to store unique elements. It describes the characteristics of mathematical sets. Therefore, it allows us to perform all these operations on mathematical sets such as union, comparison, intersection, etc. The agenda of this article is to write a Java program to compare two collections. For comparison operations between two collections, Java provides a built-in method "equals()", which we will discuss in the next section.
We will use the following classes and methods in our Java program to compare two collections -
Since Set is an interface, we cannot use its functions directly. To do this, we need a TeeSet class that implements the Set interface, which means it has access to all methods of Set. It stores all elements in the form tree structure, and like Set, it does not allow storing duplicate elements.
It is a method of Set, used to check whether two given collections contain the same number and type of objects in the same order. Returns true if both collections satisfy the condition, false otherwise.
setOne.equals(setTwo);
Now, let us enter the Java program to compare and check whether two given collections are equal or not.
The Chinese translation ofIn the following Java program, we will create two TreeSet classes with the same elements but in different order. Nonetheless, when we use equals() method for comparison, it will return true because by default TreeSet stores its elements in 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"); } } }
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
This is another Java program that demonstrates how to compare two collections using the equals() method. We first initialize the two arrays and then use the asList() method to convert them into collections so that we can compare 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"); } } }
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
We first defined the Set interface of the Java collection framework. In the next section, we wrote two Java programs to compare and check whether two given collections are equal. For this, we use the equals() method of the TreeSet class and the Set interface.
The above is the detailed content of Java program to compare two collections. For more information, please follow other related articles on the PHP Chinese website!