HashMap and HashCode

jdk hashmap hashCode

HashMap and HashCode

HashMap and HashCode

The HashMap is a class which is used to perform some basic operations such as inserting, deleting, and locating elements in a Map . The  java.util.HashMap class is implemented with and roughly equivalent to a Hashtable  except that it is unsynchronized and permits null. It works with the Iterators requires a well-defined implementation of the method hashCode( ).

HashCode is a method that returns an integer to confirm whether two objects are equal. Such objects are mainly HashMaps and HashSets. The objects are only equal if if their hashCode methods return equal integer values.

Given below example will give you a clear idea how to use HashCode :

package simpleCoreJava;

import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;

public class MapHashCode {
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("Size of map1 = " + mp1.size());
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("Size of map2 = " + mp2.size());
boolean bol = mp1.equals(mp2);
System.out.println("Is the elements of map1 and map2 are equal : "
+ bol);
System.out.println("Hash code of map1 = " + mp1.hashCode());
System.out.println("Hash code of map2 = " + mp2.hashCode());
Map mp3 = new HashMap();
mp3.put(1, "E");
mp3.put(2, "F");
mp3.put(3, "G");
mp3.put(4, "H");
System.out.println("Mapping3 = " + mp3);
System.out.println("Size of map3 = " + mp3.size());
System.out.println("Mapping1 = " + mp1);
boolean bol1 = mp1.equals(mp3);
System.out.println("Is the elements of map1 and map3 are equal : "
+ bol1);
System.out.println("Hash code of map1 = " + mp1.hashCode());
System.out.println("Hash code of map3 = " + mp3.hashCode());
System.out.println("Mapping2 = " + mp2);
System.out.println("Mapping3 = " + mp3);
boolean bol2 = mp2.equals(mp3);
System.out.println("Is the elements of map2 and map3 are equal : "
+ bol2);
System.out.println("Hash code of map2 = " + mp2.hashCode());
System.out.println("Hash code of map3 = " + mp3.hashCode());
}
}

Output

Mapping1 = {1=A, 2=B, 3=C, 4=D}
Size of map1 = 4
Mapping2 = {1=A, 2=B, 3=C, 4=D}
Size of map2 = 4
Is the elements of map1 and map2 are equal : true
Hash code of map1 = 256
Hash code of map2 = 256
Mapping3 = {1=E, 2=F, 3=G, 4=H}
Size of map3 = 4
Mapping1 = {1=A, 2=B, 3=C, 4=D}
Is the elements of map1 and map3 are equal : false                  
Hash code of map1 = 256
Hash code of map3 = 280
Mapping2 = {1=A, 2=B, 3=C, 4=D}
Mapping3 = {1=E, 2=F, 3=G, 4=H}
Is the elements of map2 and map3 are equal : false
Hash code of map2 = 256
Hash code of map3 = 280

 

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials