AbstractMap.SimpleEntry equals(Object o) Method in Java with Examples
Last Updated :
27 Jun, 2019
AbstractMap.SimpleEntry<K, V> is used to maintain a key and a value entry. The value can be changed using the equals? method. This class facilitates the process of building custom map implementations.
equals(Object o) method of AbstractMap.SimpleEntry<K, V> used to compare the specified object passed as parameter with this entry for equality.The method returns true if the given object also contains a map entry and the two entries represent the same mapping.
Two entries e1 and e2 represent the same mapping if
(e1.getKey()==null ?
e2.getKey()==null :
e1.getKey().equals(e2.getKey()))
&&
(e1.getValue()==null ?
e2.getValue()==null :
e1.getValue().equals(e2.getValue()))
Syntax:
public V equals(Object o)
Parameters: This method accepts object to be compared for equality with this map entry.
Return value: This method true if the specified object is equal to this map entry.
Below programs illustrate equals(Object o) method:
Program 1:
import java.util.*;
public class GFG {
@SuppressWarnings ({ "unchecked" , "rawtypes" })
public static void main(String[] args)
{
AbstractMap.SimpleEntry<Integer, Integer> map1
= new AbstractMap.SimpleEntry( 0 , 123 );
AbstractMap.SimpleEntry<Integer, Integer> map2
= new AbstractMap.SimpleEntry( 0 , 123 );
boolean answer = map1.equals(map2);
System.out.println( "Map 1 is equal to Map2 -> "
+ answer);
}
}
|
Output:
Map 1 is equal to Map2 -> true
Program 2:
import java.util.*;
public class GFG {
@SuppressWarnings ({ "unchecked" , "rawtypes" })
public static void main(String[] args)
{
AbstractMap.SimpleEntry<String, String> map1
= new AbstractMap
.SimpleEntry<String, String>( "Captain:" , "Dhoni" );
AbstractMap.SimpleEntry<String, String> map2
= new AbstractMap
.SimpleEntry<String, String>( "Captain:" , "Kohli" );
boolean answer = map1.equals(map2);
System.out.println( "Map 1 is equal to Map2 -> "
+ answer);
}
}
|
Output:
Map 1 is equal to Map2 -> false
References: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html##equals(java.lang.Object)
Share your thoughts in the comments
Please Login to comment...