GeeksforGeeks App
Open App
Browser
Continue

# SortedMap equals() method in Java with Examples

In Java, SortedMap is an interface that extends the Map interface and maintains its key-value pairs in ascending order based on the keys’ natural ordering or a custom comparator. The equals(Object obj) method of the SortedMap interface is used to compare the SortedMap to the specified object for equality.

The equals(Object obj) method returns true if the specified object is also a SortedMap and has the same mappings as the SortedMap being compared, and false otherwise. Here’s an example of how to use the equals() method:

## Java

 `import` `java.util.SortedMap;``import` `java.util.TreeMap;` `public` `class` `SortedMapExample {``  ``public` `static` `void` `main(String[] args) {``    ``// Create two SortedMaps and add some key-value pairs``    ``SortedMap sortedMap1 = ``new` `TreeMap<>();``    ``sortedMap1.put(``"John"``, ``30``);``    ``sortedMap1.put(``"Alice"``, ``25``);``    ``sortedMap1.put(``"Bob"``, ``40``);` `    ``SortedMap sortedMap2 = ``new` `TreeMap<>();``    ``sortedMap2.put(``"Alice"``, ``25``);``    ``sortedMap2.put(``"Bob"``, ``40``);``    ``sortedMap2.put(``"John"``, ``30``);` `    ``// Compare the two SortedMaps for equality``    ``boolean` `areEqual = sortedMap1.equals(sortedMap2);` `    ``System.out.println(``"Are the two SortedMaps equal? "` `+ areEqual);``  ``}``}`

Output

`Are the two SortedMaps equal? true`

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:

## Java

 `// 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 map1``            ``= ``new` `TreeMap();``        ``SortedMap map2``            ``= ``new` `TreeMap();` `        ``// 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));``    ``}``}`

Program 2:

## Java

 `// 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 map1``            ``= ``new` `TreeMap();``        ``SortedMap map2``            ``= ``new` `TreeMap();` `        ``// 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));``    ``}``}`

My Personal Notes arrow_drop_up
Related Tutorials