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.

  1. import java.util.Collection;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import java.util.Map.Entry;
  8.  
  9. public class LinkedHashMapImpl<K, V>
  10. { 
  11.     private LinkedHashMap<K, V> linkedHashMap;
  12.  
  13.     /*
  14.      * Constructs an empty insertion-ordered LinkedHashMap instance with the
  15.      * default initial capacity (16) and load factor (0.75).
  16.      */
  17.     public LinkedHashMapImpl()
  18.     {
  19.         linkedHashMap = new LinkedHashMap<K, V>();
  20.     }
  21.  
  22.     /*
  23.      * Constructs an empty insertion-ordered LinkedHashMap instance with the
  24.      * specified initial capacity and a default load factor (0.75).
  25.      */
  26.     public LinkedHashMapImpl(int initialCapacity)
  27.     {
  28.         linkedHashMap = new LinkedHashMap<K, V>(initialCapacity);
  29.     }
  30.  
  31.     /*
  32.      * Constructs an empty insertion-ordered LinkedHashMap instance with the
  33.      * specified initial capacity and load factor.
  34.      */
  35.     public LinkedHashMapImpl(int initialCapacity, float loadFactor)
  36.     {
  37.         linkedHashMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor);
  38.     }
  39.  
  40.     /*
  41.      * Constructs an empty LinkedHashMap instance with the specified initial
  42.      * capacity, load factor and ordering mode.
  43.      */
  44.     public LinkedHashMapImpl(int initialCapacity, float loadFactor, boolean accessOrder)
  45.     {
  46.         linkedHashMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor, accessOrder);
  47.     }
  48.  
  49.     /*
  50.      * Constructs an insertion-ordered LinkedHashMap instance with the same
  51.      * mappings as the specified map.
  52.      */
  53.     public LinkedHashMapImpl(Map<? extends K, ? extends V> m)
  54.     {
  55.         linkedHashMap = new LinkedHashMap<K, V>(m);
  56.     }
  57.  
  58.     /* Removes all of the mappings from this map. */
  59.     public void clear()
  60.     {
  61.         linkedHashMap.clear();
  62.     }
  63.  
  64.     /*
  65.      * Returns a shallow copy of this LinkedHashMap instance: the keys and values
  66.      * themselves are not cloned.
  67.      */
  68.     public Object clone()
  69.     {
  70.         return linkedHashMap.clone();
  71.     }
  72.  
  73.     /* Returns true if this map contains a mapping for the specified key. */
  74.     public boolean containsKey(Object key)
  75.     {
  76.         return linkedHashMap.containsKey(key);
  77.     }
  78.  
  79.     /* Returns true if this map maps one or more keys to the specified value. */
  80.     public boolean containsValue(Object value)
  81.     {
  82.         return linkedHashMap.containsValue(value);
  83.     }
  84.  
  85.     /* Returns a Set view of the mappings contained in this map. */
  86.     public Set<Map.Entry<K, V>> entrySet()
  87.     {
  88.         return linkedHashMap.entrySet();
  89.     }
  90.  
  91.     /*
  92.      * Returns the value to which the specified key is mapped, or null if this
  93.      * map contains no mapping for the key.
  94.      */
  95.     public V get(Object key)
  96.     {
  97.         return linkedHashMap.get(key);
  98.     }
  99.  
  100.     /* Returns true if this map contains no key-value mappings. */
  101.     public boolean isEmpty()
  102.     {
  103.         return linkedHashMap.isEmpty();
  104.     }
  105.  
  106.     /* Returns a Set view of the keys contained in this map. */
  107.     public Set<K> keySet()
  108.     {
  109.         return linkedHashMap.keySet();
  110.     }
  111.  
  112.     /* Associates the specified value with the specified key in this map. */
  113.     public V put(K key, V value)
  114.     {
  115.         return linkedHashMap.put(key, value);
  116.     }
  117.  
  118.     /* Copies all of the mappings from the specified map to this map. */
  119.     public void putAll(Map<? extends K, ? extends V> m)
  120.     {
  121.         linkedHashMap.putAll(m);
  122.     }
  123.  
  124.     /* Removes the mapping for the specified key from this map if present. */
  125.     public V remove(Object key)
  126.     {
  127.         return linkedHashMap.remove(key);
  128.     }
  129.  
  130.     /* Returns the number of key-value mappings in this map. */
  131.     public int size()
  132.     {
  133.         return linkedHashMap.size();
  134.     }
  135.  
  136.     /* Returns a Collection view of the values contained in this map. */
  137.     public Collection<V> values()
  138.     {
  139.         return linkedHashMap.values();
  140.     }
  141.  
  142.     public static void main(String... arg)
  143.     {
  144.         LinkedHashMapImpl<Integer, Integer> linkedHashMap = new LinkedHashMapImpl<Integer, Integer>();
  145.         linkedHashMap.put(1, 100);
  146.         linkedHashMap.put(2, 200);
  147.         linkedHashMap.put(3, 300);
  148.         linkedHashMap.put(4, 400);
  149.  
  150.         Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
  151.         linkedHashMap.putAll(anotherMap);
  152.         System.out.println("the key set of the linked hashmap is ");
  153.         Set<Integer> keySet = linkedHashMap.keySet();
  154.         Iterator<Integer> itr = keySet.iterator();
  155.         while (itr.hasNext())
  156.         {
  157.             System.out.print(itr.next() + "\t");
  158.         }
  159.         System.out.println();
  160.  
  161.         System.out.println("the values of the linkedhash map is ");
  162.         Collection<Integer> collectionValues = linkedHashMap.values();
  163.         itr = collectionValues.iterator();
  164.         while (itr.hasNext())
  165.         {
  166.             System.out.print(itr.next() + "\t");
  167.         }
  168.         System.out.println();
  169.  
  170.         System.out.println("the entry set of the linkedhash map is ");
  171.         Iterator<Entry<Integer, Integer>> eitr;
  172.         Set<Entry<Integer, Integer>> entrySet = linkedHashMap.entrySet();
  173.         eitr = entrySet.iterator();
  174.         while (eitr.hasNext())
  175.         {
  176.             System.out.println(eitr.next() + "\t");
  177.         }
  178.  
  179.         System.out.println("the hash Map contains Key 3 :" + linkedHashMap.containsKey(3));
  180.         System.out.println("the hash Map contains Value 600 :" + linkedHashMap.containsValue(600));
  181.         System.out.println("the size of the hash Map is " + linkedHashMap.size());
  182.         linkedHashMap.clear();
  183.         if (linkedHashMap.isEmpty())
  184.             System.out.println("the linked hash Map is empty");
  185.         else
  186.             System.out.println("the hash Map is not empty");
  187.     }
  188.  
  189. }


$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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.