Friday, October 10, 2025
HomeLanguagesJavaSortedSet equals() method in Java with Examples

SortedSet equals() method in Java with Examples

The equals() method of java.util.SortedSet class is used to verify the equality of an Object with a SortedSet and compare them. The method returns true if the size of both the SortedSets are equal and both contain the same elements.

Syntax:  

public boolean equals(Object o)

Parameters: This method takes the object o as a parameter to be compared for equality with this set.

Returns Value: This method returns true if the specified object is equal to this set.

Note: The equals() method in SortedSet is inherited from the Set interface in Java.

Below are the examples to illustrate the equals() method.

Example 1: 

Java




// Java program to demonstrate equals()
// method of SortedSet
 
import java.util.*;
 
public class GFG {
    public static void main(String[] argv)
    {
 
        // Creating object of Set
        SortedSet<String> arrset1
            = new TreeSet<String>();
 
        // Populating arrset1
        arrset1.add("A");
        arrset1.add("B");
        arrset1.add("C");
        arrset1.add("D");
        arrset1.add("E");
 
        // print arrset1
        System.out.println("First Set: "
                           + arrset1);
 
        // Creating another object of Set
        SortedSet<String> arrset2
            = new TreeSet<String>();
 
        // Populating arrset2
        arrset2.add("A");
        arrset2.add("B");
        arrset2.add("C");
        arrset2.add("D");
        arrset2.add("E");
 
        // print arrset2
        System.out.println("Second Set: "
                           + arrset2);
 
        // comparing first Set to another
        // using equals() method
        boolean value
            = arrset1.equals(arrset2);
 
        // print the value
        System.out.println("Are both set equal? "
                           + value);
    }
}


Output: 

First Set: [A, B, C, D, E]
Second Set: [A, B, C, D, E]
Are both set equal? true

 

Example 2: 

Java




// Java program to demonstrate
// equals() method of Sorted Set
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
 
        // Creating object of Set
        SortedSet<Integer> arrset1
            = new TreeSet<Integer>();
 
        // Populating arrset1
        arrset1.add(10);
        arrset1.add(20);
        arrset1.add(30);
        arrset1.add(40);
        arrset1.add(50);
 
        // print arrset1
        System.out.println("First Set: "
                           + arrset1);
 
        // Creating another object of Set
        SortedSet<Integer> arrset2
            = new TreeSet<Integer>();
 
        // Populating arrset2
        arrset2.add(10);
        arrset2.add(20);
        arrset2.add(30);
 
        // print arrset2
        System.out.println("Second Set: "
                           + arrset2);
 
        // comparing first Set to another
        // using equals() method
        boolean value
            = arrset1.equals(arrset2);
 
        // print the value
        System.out.println("Are both set equal? "
                           + value);
    }
}


Output:

First Set: [10, 20, 30, 40, 50]
Second Set: [10, 20, 30]
Are both set equal? false

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#equals(java.lang.Object) 
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS