Topics Index

Linked Hash Map - Java Example Program

Linked Hash Map
import java.util.*;

class LinkedHashMapTest
    public static void main(String arg[])
        LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
        lhm.put("Ramesh", "Intermediate");
        lhm.put("Shiva", "B-Tech");
        lhm.put("Santosh", "B-Com");
        lhm.put("Asha", "Msc");
        lhm.put("Raghu", "M-Tech");
        Set set = lhm.entrySet();
        Iterator i = set.iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry);
            System.out.println(me.getKey() + " : " + me.getValue());
        System.out.println("The Key Contains : " + lhm.containsKey("Shiva"));
        System.out.println("The value to the corresponding to key : " + lhm.get("Asha"));    

Ramesh : Intermediate
Shiva : B-Tech
Santosh : B-Com
Asha : Msc
Raghu : M-Tech
The Key Contains : true
The value to the corresponding to key : Msc


In this program, LinkedHashMap object is created of both key and value types are String. Five records are inserted into the LinkedHashMap and then iterating the set LinkedHashMap using Map.Entry and printing the key and values as inserted into it.

  • Take LinkedHashMap type of key String and value of type Integer for adding student details given below and print the LinkedHashMap.
    [Raja=50, Sareesh=45, Hemanth=40, Srinath=50]
  • If you provide key which does not exists in LinkedHashMap at containsKey null will be returned. Try for the below code.
  • You can apply Map and Hash Map methods to LinkedHashMap.

© meritcampus 2019

All Rights Reserved.

Open In App