LinkedHashMap implementation in java

               It is a type of map which extends ‘Map’ from java collections Framework. Here, a doubly linked list is used. This uses either insertion-order or access order to iterate the linked list.

Features:

  • ·       The iteration follows either insertion order or access order.
  • ·       The performance is balanced among the prediction of iterations and operations.
  • ·       It allows one null key and multiple null values.

Implementation of LinkedHashMap in java:

              This program creates some data structures with number of elements for mapping.

Steps:

  • ·       First, import the built in packages java.util.LinkedHashMap and java.util.Map.
  • ·       Create a public class with main() function.
  • ·       An object instance is created for LinkedHashMap.
  • ·       Using the object, the key,value pair is added to the linkedHashMap.
  • ·       Function get() is used to retrieve and display the key,value pair.
  • ·       ContainsKey() member function finds a key,value pair is available in the map.
  • ·       getKey(),getValue() retrive the key,value from the map.
  • ·       To delete a key,value pair, remove() function is used.
  • ·       Clear() function clears the entire elements.
  • ·       isEmpty() returns true when the map is empty.

Program:

import java.util.LinkedHashMap;

import java.util.Map;

public class LinkedHashMapEg {

    public static void main(String[] args) {

        // Create an object for LinkedHashMap

        Map<String, Integer> linkedHashMap1 = new LinkedHashMap<>();

        // insert the key-value pairs to the linkedHashMap

        linkedHashMap1.put("LinkedList", 50);

        linkedHashMap1.put("Stack", 20);

        linkedHashMap1.put("Queue", 30);

        linkedHashMap1.put("Tree", 40);

        linkedHashMap1.put("Binary Tree",45);

        // Extract a data

        System.out.println("Number of elements in Queue: " + linkedHashMap1.get("Queue"));

        // Check a key's availability

        if (linkedHashMap1.containsKey("Tree")) {

            System.out.println("Tree is available.");

        }

        // Create a loop to display the key,value in LinkedHashMap

        for (Map.Entry<String, Integer> entry : linkedHashMap1.entrySet()) {

            System.out.println(entry.getKey() + ": " + entry.getValue());

        }

         // Delete a key-value pair

        linkedHashMap1.remove("Binary Tree");

        // Print the size of the LinkedHashMap

        System.out.println("Size of the LinkedHashMap: " + linkedHashMap1.size());

        // Clear all the key,value pair in the LinkedHashMap

        linkedHashMap1.clear();

        // Check the linkedHashMap is empty or not

        if(linkedHashMap1.isEmpty())

        System.out.println("LinkedHashMap is empty");

    }

}

Output:

C:\raji\blog>javac LinkedHashMapEg.java

C:\raji\blog>java LinkedHashMapEg

Number of elements in Queue: 30

Tree is available.

LinkedList: 50

Stack: 20

Queue: 30

Tree: 40

Binary Tree: 45

Size of the LinkedHashMap: 4

LinkedHashMap is empty

This is the simple way of implementing LinkedHashMap program in java. Hope, this post will be useful for you. Keep coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.