Java Program to Implement LinkedHashMap API Last Updated : 28 Jan, 2021 Comments Improve Suggest changes Like Article Like Report The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. To implement LinkedHashMap API first we create a class “ LinkedHashMapImplmentation” and create all the methods of the LinkedHashMap in this class. Implementation of the LinkedHashMap API: Java // Java program demonstrate LinkedHashMap API import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; class LinkedHashMapImplementation<K, V> { private LinkedHashMap<K, V> map; // Constructor creates a new LinkedHashMap public LinkedHashMapImplementation() { map = new LinkedHashMap<K, V>(); } // Constructor creates a new empty linkedhash map // according to the given map public LinkedHashMapImplementation( Map<? extends K, ? extends V> map1) { map = new LinkedHashMap<K, V>(map1); } // Delete all the key-value pair public void clear() { map.clear(); } // Returns a shallow copy of the LinkedHashMap instance public Object clone() { return map.clone(); } // Returns true if the map contains the specified key public boolean containsKey(Object key) { return map.containsKey(key); } // Returns true if map contains specified value public boolean containsValue(Object val) { return map.containsValue(val); } // Returns a Set view of the mappings public Set<Map.Entry<K, V> > entrySet() { return map.entrySet(); } // Returns the value to which the specified key is // mapped, returns null if map does not contains given // key public V get(Object key) { return map.get(key); } // Returns a Set view of the keys public Set<K> keySet() { return map.keySet(); } // Associates the specified value with the specified key // in the map public V put(K key, V val) { return map.put(key, val); } // Copies all of the mappings from the specified map to // the map public void putAll(Map<? extends K, ? extends V> map1) { map.putAll(map1); } // Removes the mapping for the key from the // LinkedHashMap if present public V remove(Object key) { return map.remove(key); } // Returns the size of the map public int size() { return map.size(); } // Returns a Collection view of the values public Collection<V> values() { return map.values(); } } public class GFG { public static void main(String[] arg) { LinkedHashMapImplementation<String, Integer> student = new LinkedHashMapImplementation<>(); // Add elements to the LinkedHashMap student.put("Anuj", 500); student.put("Ashok", 460); student.put("Aakansha", 495); // Print the size of the LinkedHashMap System.out.println("Size of the LinkedHashMap: " + student.size()); System.out.println(); System.out.println( "The key value pairs of LinkedHashMap:"); // Print the key value pairs of LinkedHashMap for (Map.Entry<String, Integer> entry : student.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } System.out.println(); // Print LinkedHashMap contains key Aakansha or not System.out.println( "LinkedHashMap contains Aakansha: " + student.containsKey("Aakansha")); System.out.println(); // Print LinkedHashMap contains value 450 or not System.out.println("LinkedHashMap contains 450: " + student.containsValue(450)); System.out.println(); // Deletes all the entry student.clear(); System.out.println("Size of the LinkedHashMap: " + student.size()); } } OutputSize of the LinkedHashMap: 3 The key value pairs of LinkedHashMap: Anuj : 500 Ashok : 460 Aakansha : 495 LinkedHashMap contains Aakansha: true LinkedHashMap contains 450: false Size of the LinkedHashMap: 0 Comment More infoAdvertise with us Next Article Java Program to Implement LinkedHashMap API N nikhilchhipa9 Follow Improve Article Tags : Java Java Programs Java-Collections Java-LinkedHashMap Practice Tags : JavaJava-Collections Similar Reads Java Program to Implement LinkedHashSet API The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained, this class is used. When iterating through a HashSet, the order is unpredictable, while a LinkedHashSet lets us iterate through the element 4 min read Java Program to Iterate LinkedHashSet Elements The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 3 min read Java Program to Implement HashTable API The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. To implement Hashtable API f 4 min read How to Remove Elements from a LinkedHashMap in Java? A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove 2 min read Java Program to Sort LinkedHashMap By Values The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion, but it never maintained the track and order of insertion which the LinkedHashMap provides where the element 3 min read Java Program to Implement WeakHashMap API The WeakHashMap class is part of the Java Collections Framework, implementing the Map interface and extending the AbstractMap class. The Map interface helps us map key to values, while the AbstractMap class makes implementing the Map interface easier. Every key in a map must be unique and can unite 7 min read Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde 4 min read Java Program to Implement HashTables with Linear Probing Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects. Suppose an object is to be assigned a key to it to make searching easy. To store the key/value pair, one can use a simple array like a data structure where keys (integers) can be used directly 5 min read How to iterate LinkedHashMap in Java? LinkedHashMap class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted. Th 2 min read How to Merge Two LinkedHashMaps in Java? In Java, LinkedHashMap is a class that extends HashMap and maintains the order of elements based on the order of insertion. Merging two LinkedHashMaps involves combining their key-value pairs while ensuring that the order is preserved. In, this article we will explore different approaches to merging 3 min read Like