SortedMap equals() method in Java with Examples

The equals() method in SortedMap Java is used to check for equality between two SortedMap instances. It verifies whether the elements of one SortedMap passed as a parameter is equal to the elements of this SortedMap or not.

Syntax:

boolean equals(object obj)

Parameters: The method accepts one parameter obj of this map type and refers to the map whose equality is to be checked with this map.



Return Value: The method returns true if the equality holds for both the object map else it returns false.

Note: The equals() method in SortedMap is inherited from the Map interface in Java.

Below programs illustrate the equals() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate
// the equals() method
  
import java.util.*;
  
public class Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty SortedMap
        SortedMap<Integer, String> map1
            = new TreeMap<Integer, String>();
        SortedMap<Integer, String> map2
            = new TreeMap<Integer, String>();
  
        // Mapping string values to int keys
        map1.put(10, "Geeks");
        map1.put(15, "4");
        map1.put(20, "Geeks");
        map1.put(25, "Welcomes");
        map1.put(30, "You");
  
        // Mapping string values to int keys
        map2.put(10, "Geeks");
        map2.put(15, "4");
        map2.put(20, "Geeks");
        map2.put(25, "Welcomes");
        map2.put(30, "You");
  
        // Displaying the Map1
        System.out.println("First Map: "
                           + map1);
  
        // Displaying the Map2
        System.out.println("Second Map: "
                           + map2);
  
        // Checking the equality
        System.out.println(
            "Equality: "
            + map1.equals(map2));
    }
}

chevron_right


Output:

First Map:
 {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Second Map:
 {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Equality: true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate
// the equals() method
  
import java.util.*;
  
public class Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty SortedMap
        SortedMap<Integer, String> map1
            = new TreeMap<Integer, String>();
        SortedMap<Integer, String> map2
            = new TreeMap<Integer, String>();
  
        // Mapping string values
        // to int keys for map1
        map1.put(10, "Geeks");
        map1.put(15, "4");
        map1.put(20, "Geeks");
        map1.put(25, "Welcomes");
        map1.put(30, "You");
  
        // Mapping string values
        // to int keys for map2
        map2.put(10, "Geeks");
        map2.put(15, "4");
        map2.put(20, "Geek");
        map2.put(25, "Welcomes");
        map2.put(30, "You");
  
        // Displaying the map 1
        System.out.println("First Map: "
                           + map1);
  
        // Displaying the map 2
        System.out.println("Second Map: "
                           + map2);
  
        // Displaying the equality
        System.out.println("Equality: "
                           + map1.equals(map2));
    }
}

chevron_right


Output:

First Map: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Second Map: {10=Geeks, 15=4, 20=Geek, 25=Welcomes, 30=You}
Equality: false

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



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.