How to Add Key-Value pairs to LinkedHashMap in Java? Last Updated : 23 Jul, 2025 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 R rbbansal Follow Improve Article Tags : Java Java Programs Java-LinkedHashMap Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like