Wednesday, 16 April 2014

hashCode : hash code value for this map

int hashCode()
Returns the hash code value for this map. he hash code of a map is defined to be the sum of the hash codes of each entry in the map's.

import java.util.*;
class MapHashCode{
  public static void main(String args[]){
        Map<Integer, String> myMap = new HashMap<> ();
  
  /* Put the data to Map */
  myMap.put(1, "First");
  myMap.put(2, "Second");
  myMap.put(3, "Third");
  myMap.put(4, "Fourth");
  
  int i = 1;
  int sum = 0;
  
  for(Map.Entry<Integer, String> entry1 : myMap.entrySet()){
  System.out.print("\nHash Code Of Entry ");
  System.out.println(entry1 + " is " + entry1.hashCode());
  sum = sum + entry1.hashCode();
  }
  
  System.out.println("\nSum Of All Hash codes is " + sum);
  System.out.println("Hash code Of the map is " + myMap.hashCode());
  }
}

Output
Hash Code Of Entry 1=First is 67887761

Hash Code Of Entry 2=Second is -1822412650

Hash Code Of Entry 3=Third is 80778564

Hash Code Of Entry 4=Fourth is 2110150206

Sum Of All Hash codes is 436403881
Hash code Of the map is 436403881


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment