This method gives back 'true' if the given object would be a map and when equivalence with the other map it should represents the same mappings otherwise, returns 'false'.
equals(Object o) method of Map Interface in Java.
In this section we will discuss how can equals(Object o) method be implemented in Map interface in java.
Syntax
boolean equals(Object o)
This method processes an equivalence comparison for a specific object within the underlying map.
This method gives back 'true' if the given object would be a map and when equivalence with the other map it should represents the same mappings otherwise, returns 'false'.
Parameter description
o : It takes an object to be compared for equality with the underlying map.
Example of equals(Object o) method
In this example we will show you how does equals(Object o) method work in Map interface. This example will help you to understand how you can compare the two maps. In the following example we will also show you what does this method return when the two maps are getting equals.
Example :
package devmanuals.com; import java.util.Map; import java.util.HashMap; import java.util.TreeMap; public class MapEquals { public static void main (String args[]){ Map mp1=new HashMap(); mp1.put(1, "A"); mp1.put(2, "B"); mp1.put(3, "C"); mp1.put(4, "D"); System.out.println("Mapping1 = "+mp1); System.out.println("The key set of map1= "+mp1.keySet()); System.out.println("The values of map1= "+mp1.values()); Map mp2= new TreeMap(); mp2.put(1, "A"); mp2.put(2, "B"); mp2.put(3, "C"); mp2.put(4, "D"); System.out.println("Mapping2 = "+mp2); System.out.println("The key set of map2= "+mp2.keySet()); System.out.println("The values of map2= "+mp2.values()); boolean bol= mp1.equals(mp2); System.out.println("Is the both map are equals : "+bol); } }
Output :
Mapping 1= {1=A, 2=B, 3=C, 4=D} The key set of map1= [1, 2, 3, 4] The values of map1= [A, B, C, D] Mapping2 = {1=A, 2=B, 3=C, 4=D} The key set of map2= [1, 2, 3, 4] The values of map2= [A, B, C, D] Is the both map are equals : true |
[ 0 ] Comments