HashMap in Java

A HashMap implements Map interface.In a Map structure  , data is storing as key value pairs.HashMap is simply unordered and unsorted map structure.The hasCode() method plays an important role when we are using objects of our own classes as keys in a HashMap . The efficiency of our structure depends on the hashCode() implementation of the class. We should override the hashCode() and equals() methods too.For more details about hashCode() and equals() Click here.

Now let us see an example class which uses HashMap for storing data.This example shows how we can put , iterate and delete data  from a HashMap. Here  we are using  String objects as keys as well as values.

As we discussed , the data is storing in a  HashMap in random order.The order of insertion is not preserving.This can be verified after analyzing the output.

Output

Adding contents to map

Displaying contents of map

Size = 5

sheep4: sheep

cat1: cat

cow2: cow

elephant3: elephant

dog0: dog

Displaying contents after removal:

Displaying contents of map

Size = 0

So this verifies our concept.

Objects of our own classes as keys in HashMap

As we already discussed  efficiency of a HashMap depends on the hashCode() implementation . If we want to use objects of our own classes as keys then we should override hashCode() method . If we are not overriding the hashCode() method , that means we are using the hashCode() implementation of  the super class (java.lang.Object).This default implementation gives different hash code values for all the objects.Even the meaningfully same objects are getting two hash codes. So meaningfully same objects will be treated as different objects. So we should override the hashCode() method in our class in such a way that such meaningfully same objects are getting the hash code value.In addition to overriding the hashCode() we need to override the equals() method too .Let us examine this concept with suitable examples.

case 1. Without overriding hashCode() and equals()

So as explained earier , here we are using the default hashCode() and equals() of java.lang.Object super class. So even if create two objects with exactly same attribute values , both are getting different hash codes. So two objects are treating as different. If  try to add these objects as keys in HashMap , then two objects will be added as keys.It is a duplication. Because those two keys are  having exactly same attribute values ,which means  those objects are meaningfully equal.

Also if we are using the default implementation of hashCode()  and equals() , then searching for a particular key is also not effective.

Now let us see our Student.java class.Objects of this class needs to be used as keys in our HashMap.

Now let us see our Main.java.

In the addItems() method , our student1 and student3 objects are meaningfully same.But we are not overriding hashCode() .So both will be treated as different objects.So size of Map will be 3 .Also in display() method we are creating an object its attribute values are same as that of one existing in the Map.But both those objects are having different hash codes.So the search also returns false.Let us verify the output.

Output

Size of Map = 3

Is present or not ? false

case 2.By  overriding hashCode() and equals()

Now let us change our Student.java by overriding hashCode() and equals() methods.It never permits addition of duplicate keys.Also search with meaningfully same object  of one existing in Map also returns true.

Use the same Main.java of case 1 here also .Now run the Main.java.The output makes our assumptions strong.

Output

Size of Map = 2

Is present or not ? true

See Related Discussions:

Collections Framework in Java

TreeMap

Hashcode and equals in Java

ArrayList

TreeSet

Tagged with 

One thought on “HashMap in Java

  1. […] a)HashMap : It is an unsorted and unordered map implementation. In Hashset data is storing in a random order of keys.The efficiency of HashMap depends on the  hashCode() implementation of objects using as keys.HashMap allowas one null key and many null values. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">