Java EnumMap hashCode() Method



Description

The Java EnumMap hashCode() method returns the hash code value for this map. The hash code of a map is set to be the sum of the hash codes of each entry in the map.

Declaration

Following is the declaration for java.util.EnumMap.hashCode() method

public int hashCode()

Parameters

NA

Return Value

This method returns the hash code value for this map.

Exception

NA

Getting HashCode of an EnumMap of Enum, Integer Pairs Example

The following example shows the usage of Java EnumMap hashCode() method to get a hashcode of the EnumMap instance based on a key passed. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added and enumMap is printed. Using hashCode() method, the hashcode of enumMap is printed.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,Integer> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, 1);
      map.put(Numbers.TWO, 2);
      map.put(Numbers.THREE,3);
      map.put(Numbers.FOUR, 4);

      // print the whole map
      System.out.println(map); 

      // get and print the hashcode of the map
      System.out.println(map.hashCode());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3, FOUR=4}
191994402

Getting HashCode of an EnumMap of Enum, String Pairs Example

The following example shows the usage of Java EnumMap hashCode() method to get a hashcode of the EnumMap instance based on a key passed. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Strings. Few entries are added and enumMap is printed. Using hashCode() method, the hashcode of enumMap is printed.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,String> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, "1");
      map.put(Numbers.TWO, "2");
      map.put(Numbers.THREE,"3");
      map.put(Numbers.FOUR, "4");

      // print the whole map
      System.out.println(map); 

      // get and print the hashcode of the map
      System.out.println(map.hashCode());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3, FOUR=4}
191994306

Getting HashCode of an EnumMap of Enum, Object Pairs Example

The following example shows the usage of Java EnumMap hashCode() method to get a hashcode of the EnumMap instance based on a key passed. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Student objects. Few entries are added and enumMap is printed. Using hashCode() method, the hashcode of enumMap is printed.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,Student> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, new Student(1, "Julie"));
      map.put(Numbers.TWO, new Student(2, "Robert"));
      map.put(Numbers.THREE,new Student(3, "Adam"));
      map.put(Numbers.FOUR, new Student(4, "Jene"));

      // print the whole map
      System.out.println(map); 

      // get and print the hashcode of the map
      System.out.println(map.hashCode()); 
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public int hashCode() {
      return rollNo + name.hashCode();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ], FOUR=[ 4, Jene ]}
1565012393
java_util_enummap.htm
Advertisements