Home > Java > javaTutorial > Java program to compare two collections

Java program to compare two collections

WBOY
Release: 2023-09-12 08:21:02
forward
826 people have browsed it

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.

Java program compares two collections

We will use the following classes and methods in our Java program to compare two collections -

TreeSet Class

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.

equals() method

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.

grammar

setOne.equals(setTwo);
Copy after login

Now, let us enter the Java program to compare and check whether two given collections are equal or not.

The Chinese translation of

Example 1

is:

Example 1

In 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");
      }
   }
}
Copy after login

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
Copy after login

Example 2

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");
      }
   }
}
Copy after login

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
Copy after login

in conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template