AbstractMap.SimpleEntry hashCode() Method in Java with Examples

AbstractMap.SimpleEntry<K, V> is used to maintain a key and a value entry. The value can be changed using the setValue method. This class facilitates the process of building custom map implementations.

hashCode() method of AbstractMap.SimpleEntry<K, V> return the hash code value for this map entry.
The hash code for the map entry e can be calculated by:

(e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
   (e.getValue()==null ? 0 : e.getValue().hashCode())

Syntax:



public int hashCode()

Parameters: This method accepts nothing.

Return value: This method return the hash code value for this map entry.

Below programs illustrate hashCode() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// AbstractMap.SimpleEntry.hashCode() method
  
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
  
public class GFG {
  
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
  
        // create a ArrayList of Map
        ArrayList<AbstractMap
                      .SimpleEntry<Integer, Integer> >
            arrayList
            = new ArrayList<AbstractMap
                                .SimpleEntry<Integer, Integer> >();
  
        // add values
        arrayList.add(new AbstractMap.SimpleEntry(0, 123));
        arrayList.add(new AbstractMap.SimpleEntry(1, 130));
        arrayList.add(new AbstractMap.SimpleEntry(2, 994));
  
        // print keys
        for (int i = 0; i < arrayList.size(); i++) {
  
            // get map from list
            AbstractMap.SimpleEntry<Integer, Integer>
                map
                = arrayList.get(i);
  
            // get value from map using hashCode()
            int value = map.hashCode();
  
            System.out.println("HashCode = " + value);
        }
    }
}

chevron_right


Output:

HashCode = 123
HashCode = 131
HashCode = 992

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// AbstractMap.SimpleEntry.hashCode() method
  
import java.util.*;
  
public class GFG {
  
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
  
        // create a ArrayList of Map
        ArrayList<AbstractMap
                      .SimpleEntry<String, String> >
            arrayList
            = new ArrayList<AbstractMap
                                .SimpleEntry<String, String> >();
  
        // add values
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 001AB ", " Emp 1"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 011AC ", " Emp 2"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 111AD ", " Emp 3"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 101BE ", " Emp 4"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 110CE ", " Emp 5"));
  
        // print keys
        for (int i = 0; i < arrayList.size(); i++) {
  
            // get map from list
            AbstractMap.SimpleEntry<String, String>
                map
                = arrayList.get(i);
  
            // get hashcode value from map using hashCode()
            int value = map.hashCode();
  
            System.out.println("hashCode = " + value);
        }
    }
}

chevron_right


Output:

hashCode = -873386249
hashCode = -874293612
hashCode = -910934441
hashCode = -910552137
hashCode = -910906733

References: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#hashCode()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.