Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Linkedhashmap Class Java Examples

Last Updated on March 10, 2022 by Ria Pathak


LinkedHashMap is one of the most popular map implementations in Java, it extends the HashMap class and implements Map implementation of Java Collection utilities. It provides a very powerful tool to store and access data with efficiency. It provides easy and quick insertion, deletion, searching, etc.

Properties of LinkedHashMap

  • It stores key-value pairs as elements and implements the Map interface of the Java utility class
  • Only unique elements can be inserted
  • LinkedHashMap can have only one null key but multiple null values
  • It is quite the same as HashMap and non-synchronized too and also maintains the insertion order.
    The general declaration of LinkedHashMap is given as
    public class LinkedHashMap extends HashMap implements Map

Let’s start with the creation of this map.
As you can understand we have to first create an object of LinkedHashMap class that in turn will extend various already defined functions from the hashmap class. This is quite similar to its declaration given above. The very next and most important task that we will discuss is the insertion into the map.

  • Inserting elements: We will use the put method to insert elements into the LinkedHashMap.
    LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
    lhm.put(1, "PrepBytes");
    lhm.put(2, “Placement Programe");
  • Modifying elements: If we want to update any key-value pair then we simply insert the modified element with the key that has to be modified. Let me give you an example –
    In the previous insertion example, now if we put - lhm.put(2,” preparation”), this will modify the previous value with key = 2 to nee value that we have inserted.
  • Removing an element from the LinkedHashmap: It’s easier to remove any element from the LinkedHashMap, we just have to use the remove() function with the key that we want to remove. For example, if we want to remove the key-value pair with key 2 then, we use lhm.remove(2), which will remove the element from the LinkedHashMap.
  • The next most important function is to iterate through the map which can be done by a for-each loop. Let me explain this with the code implementation

Linkedhashmap Class Java Examples

import java.util.*;
class PrepBytes {
	public static void main(String args[])
		LinkedHashMap lhm = new LinkedHashMap();

		lhm.put(1, "PrepBytes");
		lhm.put(2, "Preparation");

		for (Map.Entry mapElement : lhm.entrySet()) {
			Integer key = mapElement.getKey();
			String value = mapElement.getValue();
			System.out.println(key + " -> " + value);

1 -> PrepBytes
2 -> Preparation

Key Takeaways

This brief blog tried to explain the brief use cases of LinkedHashMap and its implementation starting from its declaration, insertion of elements, removal, and traversal of the LinkedhashMap. It also gave a snippet of the application of the LinkedHashMap. To practice problems using LinkedHashMap you can check out PrepBytes – PrepBytes

Leave a Reply

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