Easy Tutorial
For Competitive Exams

Java Programming LinkedHashMap

What is LinkedHashMap?

LinkedHashMap is a combination of Hash table and linked list implementation of the Map interface, with predictable iteration order.

It maintains a doubly-linked list running through all of its entries.

The iteration order is normally the order in which keys were inserted into the map i.e FIFO.

The insertion order is not affected if a key is re-inserted into the map.

This class is different from both HashMap and TreeMap:

  • HashMap doesn’t maintain any order.
  • TreeMap sort the entries in ascending order of keys.
  • LinkedHashMap maintains the insertion order.

How to perform Basic Operations in LinkedHashMap?

The Basic Operations are:

  • create object for LinkedHashMap
  • adding elements
  • getting size
  • checking empty or not
  • deleting an element

Basic Operations

import java.util.LinkedHashMap;
 
public class Basic {
 
    public static void main(String a[]){
         
        LinkedHashMap lhm = new LinkedHashMap();
        lhm.put("one", "This is first element");
        lhm.put("two", "This is second element");
        lhm.put("four", "this element inserted at 3rd position");
        System.out.println(lhm);
        System.out.println("Getting value for key 'one': "+lhm.get("one"));
        System.out.println("Size of the map: "+lhm.size());
        System.out.println("Is map empty? "+lhm.isEmpty());
        System.out.println("Contains key 'two'? "+lhm.containsKey("two"));
        System.out.println("Contains value 'This is first element'? "
                            +lhm.containsValue("This is first element"));
        System.out.println("delete element 'one': "+lhm.remove("one"));
        System.out.println(lhm);
    }
}
Try it Yourself

How to iterate through LinkedHashMap?

Example

import java.util.LinkedHashMap;
import java.util.Set;
 
public class IterateDemo {
 
    public static void main(String a[]){
         
        LinkedHashMap lhm = new LinkedHashMap();
        lhm.put("one", "This is first element");
        lhm.put("two", "This is second element");
        lhm.put("four", "Element inserted at 3rd position");
        Set keys = lhm.keySet();
        for(String k:keys){
            System.out.println(k+" - "+lhm.get(k));
        }
    }
}
Try it Yourself

How to check whether the value exists or not in a LinkedHashMap?

The method containsValue() returns boolean value true if the value exists.

Example

import java.util.LinkedHashMap;
 
public class ValueCheck {
 
    public static void main(String a[]){
         
        LinkedHashMap lhm = new LinkedHashMap();
        lhm.put("one", "This is first element");
        lhm.put("two", "This is second element");
        lhm.put("four", "Element inserted at 3rd position");
        System.out.println("Map contains value 'This is first element'? "
                +lhm.containsValue("This is first element"));
    }
}
Try it Yourself

How to delete all entries from LinkedHashMap object?

By calling clear() method on LinkedHashMap object, we can delete all elements at at time.

Example

import java.util.LinkedHashMap;
 
public class Clear {
 
    public static void main(String a[]){
         
        LinkedHashMap lhm = new LinkedHashMap();
        lhm.put("one", "This is first element");
        lhm.put("two", "This is second element");
        lhm.put("four", "Element inserted at 3rd position");
        System.out.println(lhm);
        lhm.clear();
        System.out.println(lhm);
    }
}
Try it Yourself

Solve this!!

#20943,20944,20945
Share with Friends