Java Program to Implement LinkedHashMap API

This Java program is to Implement LinkedHashMap Collection API. Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)
This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap (and Hashtable), without incurring the increased cost associated with TreeMap.

Here is the source code of the Java program to Implement LinkedHashMap Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
 
public class LinkedHashMapImpl<K, V>
{ 
    private LinkedHashMap<K, V> linkedHashMap;
 
    /*
     * Constructs an empty insertion-ordered LinkedHashMap instance with the
     * default initial capacity (16) and load factor (0.75).
     */
    public LinkedHashMapImpl()
    {
        linkedHashMap = new LinkedHashMap<K, V>();
    }
 
    /*
     * Constructs an empty insertion-ordered LinkedHashMap instance with the
     * specified initial capacity and a default load factor (0.75).
     */
    public LinkedHashMapImpl(int initialCapacity)
    {
        linkedHashMap = new LinkedHashMap<K, V>(initialCapacity);
    }
 
    /*
     * Constructs an empty insertion-ordered LinkedHashMap instance with the
     * specified initial capacity and load factor.
     */
    public LinkedHashMapImpl(int initialCapacity, float loadFactor)
    {
        linkedHashMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor);
    }
 
    /*
     * Constructs an empty LinkedHashMap instance with the specified initial
     * capacity, load factor and ordering mode.
     */
    public LinkedHashMapImpl(int initialCapacity, float loadFactor, boolean accessOrder)
    {
        linkedHashMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor, accessOrder);
    }
 
    /*
     * Constructs an insertion-ordered LinkedHashMap instance with the same
     * mappings as the specified map.
     */
    public LinkedHashMapImpl(Map<? extends K, ? extends V> m)
    {
        linkedHashMap = new LinkedHashMap<K, V>(m);
    }
 
    /* Removes all of the mappings from this map. */
    public void clear()
    {
        linkedHashMap.clear();
    }
 
    /*
     * Returns a shallow copy of this LinkedHashMap instance: the keys and values
     * themselves are not cloned.
     */
    public Object clone()
    {
        return linkedHashMap.clone();
    }
 
    /* Returns true if this map contains a mapping for the specified key. */
    public boolean containsKey(Object key)
    {
        return linkedHashMap.containsKey(key);
    }
 
    /* Returns true if this map maps one or more keys to the specified value. */
    public boolean containsValue(Object value)
    {
        return linkedHashMap.containsValue(value);
    }
 
    /* Returns a Set view of the mappings contained in this map. */
    public Set<Map.Entry<K, V>> entrySet()
    {
        return linkedHashMap.entrySet();
    }
 
    /*
     * Returns the value to which the specified key is mapped, or null if this
     * map contains no mapping for the key.
     */
    public V get(Object key)
    {
        return linkedHashMap.get(key);
    }
 
    /* Returns true if this map contains no key-value mappings. */
    public boolean isEmpty()
    {
        return linkedHashMap.isEmpty();
    }
 
    /* Returns a Set view of the keys contained in this map. */
    public Set<K> keySet()
    {
        return linkedHashMap.keySet();
    }
 
    /* Associates the specified value with the specified key in this map. */
    public V put(K key, V value)
    {
        return linkedHashMap.put(key, value);
    }
 
    /* Copies all of the mappings from the specified map to this map. */
    public void putAll(Map<? extends K, ? extends V> m)
    {
        linkedHashMap.putAll(m);
    }
 
    /* Removes the mapping for the specified key from this map if present. */
    public V remove(Object key)
    {
        return linkedHashMap.remove(key);
    }
 
    /* Returns the number of key-value mappings in this map. */
    public int size()
    {
        return linkedHashMap.size();
    }
 
    /* Returns a Collection view of the values contained in this map. */
    public Collection<V> values()
    {
        return linkedHashMap.values();
    }
 
    public static void main(String... arg)
    {
        LinkedHashMapImpl<Integer, Integer> linkedHashMap = new LinkedHashMapImpl<Integer, Integer>();
        linkedHashMap.put(1, 100);
        linkedHashMap.put(2, 200);
        linkedHashMap.put(3, 300);
        linkedHashMap.put(4, 400);
 
        Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
        linkedHashMap.putAll(anotherMap);
        System.out.println("the key set of the linked hashmap is ");
        Set<Integer> keySet = linkedHashMap.keySet();
        Iterator<Integer> itr = keySet.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the values of the linkedhash map is ");
        Collection<Integer> collectionValues = linkedHashMap.values();
        itr = collectionValues.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the entry set of the linkedhash map is ");
        Iterator<Entry<Integer, Integer>> eitr;
        Set<Entry<Integer, Integer>> entrySet = linkedHashMap.entrySet();
        eitr = entrySet.iterator();
        while (eitr.hasNext())
        {
            System.out.println(eitr.next() + "\t");
        }
 
        System.out.println("the hash Map contains Key 3 :" + linkedHashMap.containsKey(3));
        System.out.println("the hash Map contains Value 600 :" + linkedHashMap.containsValue(600));
        System.out.println("the size of the hash Map is " + linkedHashMap.size());
        linkedHashMap.clear();
        if (linkedHashMap.isEmpty())
            System.out.println("the linked hash Map is empty");
        else
            System.out.println("the hash Map is not empty");
    }
 
}
$javac LinkedHashMapImpl.java
$java LinkedHashMapImpl
the key set of the linked hash map is 
1	2	3	4	
the values of the linked hash map is 
100	200	300	400	
the entry set of the linked hash map is 
1=100	
2=200	
3=300	
4=400	
the linked hash Map contains Key 3 :true
the linked hash Map contains Value 600 :false
the size of the linked hash Map is 4
the linked hash Map is empty

Related posts:

Using Java Assertions
Tips for dealing with HTTP-related problems
Spring Boot with Multiple SQL Import Files
Spring Boot - Zuul Proxy Server and Routing
Java Program to implement Circular Buffer
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Find Transpose of a Graph Matrix
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Constructor Injection in Spring with Lombok
A Guide to Iterator in Java
An Intro to Spring Cloud Vault
An Intro to Spring Cloud Security
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Perform Search in a BST
An Intro to Spring Cloud Task
Using the Not Operator in If Conditions in Java
Logging a Reactive Sequence
Assertions in JUnit 4 and JUnit 5
How to Set TLS Version in Apache HttpClient
Java Program to Emulate N Dice Roller
Java Program to Find the GCD and LCM of two Numbers
Hướng dẫn Java Design Pattern – Strategy
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Spring MVC Async vs Spring WebFlux
Java TreeMap vs HashMap
A Quick Guide to Spring Cloud Consul
Query Entities by Dates and Times with Spring Data JPA
Concurrent Test Execution in Spring 5
More Jackson Annotations
Java – Get Random Item/Element From a List