Java HashMap put() Method Last Updated : 11 Jul, 2025 Suggest changes Share 16 Likes Like Report The put() method of the Java HashMap class is used to add or update the key-value pairs in the map. If the key already exists in the map, the previous value associated with the key is replaced by the new value and If the key does not exist, the new key-value pair is added to the map.Syntax of HashMap put() Methodpublic V put(K key, V value)Parameters: Key: The key with which the specified value is associated.Value: The value to be associated with the specified key.Return Types: If we add any duplicate key, the value associated with that key gets update and the previous value is replaced.When a new key is passes to the put() method, it is added to the map and the map returns "null".Example: The below Java program demonstrates the use of put() method to insert key-value pairs into the HashMap. Java // Java program to demonstrate the // working of HashMap put() method import java.util.HashMap; public class Geeks { public static void main(String[] args) { // Create a HashMap HashMap<String, Integer> hm = new HashMap<>(); // Add key-value pairs hm.put("Java", 1); hm.put("programming", 2); hm.put("language", 3); System.out.println("HashMap: " + hm); } } OutputHashMap: {Java=1, language=3, programming=2} Create Quiz C chinmoy lenka Follow 16 Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-HashMap +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 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 Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like