LinkedHashMap in Java

LinkedHashMap in Java is a class in collections framework .LinkedHashMap implements Map interface.It is a linked list version of HashMap. As you know, a HashMap is unsorted and unordered collection.If we need to get the order of iteration same as that of order of insertion , then LinkedHashMap can be used.

Let us see an example.From the output we can understand that the order of insertion is preserved.

Now let us see the output.


Adding contents to map

Displaying contents of map

Size = 5

Item0: dog

Item1: cat

Item2: cow

Item3: elephant

Item4: sheep

Displaying contents after removal:

Displaying contents of map

Size = 0

So the order of insertion is preserved.

LinkedHashMap and hashCode() method

LinkedHashMap  is relying on hash code values of key objects.If we use objects of our own classes as keys in LinkedHashMap , then we need to override the hashCode() and equals() methods of java.lang.Object class in our class.The hashCode() method should return  same hash code for meaningfully equal objects. If we are not overriding the hashCode() ,objects with exactly same contents (meaningfullly same objects) can occur multiple times in our data structure.This is duplication .Also search for a particular object also return false outcome.This is well explained here with examples.

We are storing Student object vs String data   in our LinkedHashMap. Student object is the key for a particular String data.

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

See the first.We are not overriding the hashCode() and equals() values.

Now  let us see our

In the addItems() method. We are initializing few Student objects . And String data is storing against these Student  keys.The objects student1 and student3 are meaningfully same .But both are having different hash code values since we have not overridden the hashCode() method in meaningfully same objects will be duplicated in our LinkedHashMap.So displaying the size makes our understanding clear.Here student1 and student3 are treating as different objects though they are meaningfully same . So size will be 3.

And in the display() method we are creating an object student.Its attribute values  are exactly same as that of student1 and student3 (we inserted those two objects in addItems() method).But student , stdent1 and student3 are having different hashCodes() here .So false will be displayed.(For more details about hashCode () implementation  see my  article)


Size of Map = 3

Is present or not ? false

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

Now modify our by overriding hashCode() and equals().So the modified becomes:

Here we are getting  same hash code values for all objects. That means all objects  are storing in same memory slot.

Use the previos here also  . The student1and student3 are having same hash code values. And the equals() method gives the output in such a way that both are equal.So in addItems()  , emp3 is not taking as a key.Only student1 and student2 as taking as keys.And also in display()  method student object is having the same hash code value of student1 in addItems() method.student1 is already there in our map as key.So search for student gives true.

Let us verify the output


Size of Map = 2

Is present or not ? true

See Related Posts

Collections in Java


Hashcode and equals in Java

Comparable vs Comparator

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="">