HashMap and HashCode

jdk hashmap hashCode

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

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

 

Advertisements

Ads
Share on Google+Share on Google+

HashMap and HashCode

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDK 1.4 HashMap hashCode() ,jdk1.4,follow up to jdk,Follow-up to JDK 1.4HashMap hashCode() mystery,java tutorial  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads