How to Add Key-Value pairs to LinkedHashMap in Java? Last Updated : 22 Dec, 2020 Comments Improve Suggest changes Like Article Like Report LinkedHashMap is a Hash table and linked list implementation of the Map interface. In LinkedHashMap order of key-value pair depends on the order in which keys were inserted into the map. Insertion order does not affect if a key is reinserted into the map. Example: Input: Key: 1 Value : 1221 Key: 2 Value : 2112 Output: Keys : [1,2] Values : [1221,2112] Key-Value pairs : [1=1221, 2=2112] Methods Use: put(Key, Value): First parameter as key and second parameter as Value.keySet(): Creates a set out of the key elements contained in the hash map.values(): Create a set out of the values in the hash map. Approach: Create two-variable named as Key and ValueAccept the input from user in Key and in ValueUse put() method to add Key-Value pair inside the LinkedHashMap Below is the implementation of the above approach: Java // Java Program to add key-value // pairs to LinkedHashMap import java.util.*; public class Main { public static void main(String[] args) { // create an instance of LinkedHashMap LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>(); int num, key, val; num = 2; for (int i = 0; i < num; i++) { // Taking inputs from user key = i + 1; val = key * 10; // Add mappings using put method map.put(key, val); } // Displaying key System.out.println("Keys: " + map.keySet()); // Displaying value System.out.println("Values: " + map.values()); // Displaying key-value pair System.out.println("Key-Value pairs: " + map.entrySet()); } } OutputKeys: [1, 2] Values: [10, 20] Key-Value pairs: [1=10, 2=20] Time Complexity: O(1) Comment More infoAdvertise with us Next Article How to Add Key-Value pairs to LinkedHashMap in Java? R rbbansal Follow Improve Article Tags : Java Java Programs Java-LinkedHashMap Practice Tags : Java Similar Reads How to Convert all LinkedHashMap Values to a List in Java? The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List 2 min read How to Print all Keys of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains a key and its respective value. Unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to print all the Keys present in our LinkedHashMap in java. We have to iterate through each Key in our LinkedHas 2 min read How to Convert LinkedHashMap to List in Java? LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis 2 min read How to Get All the Values of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved. The task is to get all the values present in our LinkedHashMap that is linked with their respective key. Use Iteration or pred 4 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