Tag Archives: Java LinkedHashMap Tutorial

LinkedHashMap Tutorial Java – Complete Programming Example

In this chapter you will learn:

  • What is LinkedHashMap in Java?
  • How to iterate through LinkedHashMap?
  • Programming Example
  • What is LinkedHashMap class in Java?

    LinkedHashMap is a combination of HashTable and Linked List and it stores items based on Key/Value pair. LinkedHashMap doesn’t allow inserting duplicate element. Point about LinkedHashMap are listed below:

    1. It contains values based on key.
    2. It contains only unique elements.
    3. It maintains insertion order as the keys were inserted.
    4. It is same as HashMap instead maintains insertion order.
    5. It maintains Doubly Linked List running through of all its objects.

    How to Initialize LinkedHashMap?

    LinkedHashMap<Key,Value> map = new LinkedHashMap<Key,Value>();

    Methods and Properties

    Constructor Description
    LinkedHashMap() It is used to construct a default LinkedHashMap.
    LinkedHashMap(int capacity) It is used to initialize a LinkedHashMap with the given capacity.
    LinkedHashMap(int capacity, float fillRatio) It is used to initialize both the capacity and the fillRatio.
    LinkedHashMap(Map m) It is used to initialize the LinkedHashMap with the elements from the given Map class m.

    Methods

    Methods Description
    Object get(Object key) It is used to return the value to which this map maps the specified key.
    void clear() It is used to remove all mappings from this map.
    boolean containsKey(Object key) It is used to return true if this map maps one or more keys to the specified value.

    Programming Example

    import java.util.*;
    
    public class LinkedHashMap_Exmpl
    {
        public static void main(String[] args)
        {
            LinkedHashMap<Integer,String> Lmap=new LinkedHashMap<Integer,String>();
            Lmap.put(22,"Jack");
            Lmap.put(23,"Steven");
            Lmap.put(26,"Clark");
            Lmap.put(18, "Mathew");
            System.out.println("First Methods Iteration");
            PrintMethod1(Lmap);
            System.out.println("Second Methods Iteration");
            PrintMethod2(Lmap);
            System.out.println("Direct Printing");
            System.out.println(Lmap);
    
            //Remove an element
            System.out.println("Item removed : "+Lmap.remove(26));
            //New Item added
            System.out.println("Item Added : "+Lmap.put(19,"Sarah"));
            System.out.println(Lmap);
        }
        //Iterate Through LinkedHashMap Method 1
        public static void PrintMethod1(LinkedHashMap<Integer,String> lm)
        {
            Set set=lm.entrySet();
            Iterator itr=set.iterator();
            while(itr.hasNext())
            {
                Map.Entry me=(Map.Entry)itr.next();
                System.out.println("Key is : "+me.getKey()+" and Value is : "+me.getValue());
            }
        }
    
        //Iterate Through LinkedHashMap Method 1
        public static void PrintMethod2(LinkedHashMap<Integer,String> lm)
        {
            for(Map.Entry m:lm.entrySet())
            {
                System.out.println("Key : "+m.getKey() + " | Value = "+m.getValue());
            }
        }
    }

    Output

    First Methods Printing
    Key is : 22 and Value is : Jack
    Key is : 23 and Value is : Steven
    Key is : 26 and Value is : Clark
    Key is : 18 and Value is : Mathew
    Second Methods Printing
    Key : 22 | Value = Jack
    Key : 23 | Value = Steven
    Key : 26 | Value = Clark
    Key : 18 | Value = Mathew
    Direct Printing
    {22=Jack, 23=Steven, 26=Clark, 18=Mathew}
    Item removed : Clark
    Item Added : null
    {22=Jack, 23=Steven, 18=Mathew, 19=Sarah}
    _

    Summary

    In this chapter I’ve tried to explain LinkedHashMap with complete programming example in java. Almost the programming of all the collections classes are same and there is not too much differences. Once, you learn to write collection programming It gets very easy to learn rest of the collection objects. In the next chapter you will learn TreeMap class in Java.