How to Retrieve a Specific Value From a HashMap Using Key? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report A HashMap is a data structure that implements a map interface in Java. It stores key-value pairs. On the key object's hashcode, it uses a hashing function for computing an index-based element. And it is used to store and retrieve values. This allows very fast lookup, addition, and removal of key-value pairs, with average O(1) time complexity. In this article, we will learn how to retrieve a specific value from a HashMap using its key in Java. Syntax:HasMap.get(object key_element)Program to Retrieve a Specific Value From a HashMap Using Key in JavaBelow is the implementation of retrieving a specific value from a HashMap using its key: Java // Java Program to to retrieve a specific value from a HashMap using its key import java.util.HashMap; public class Main { public static void main(String[] args) { // Create a HashMap HashMap<String, Integer> courses = new HashMap<>(); // Add key-value pairs courses.put("Java", 30000); courses.put("C++", 25000); courses.put("Python", 35000); // Retrieve value using key String key = "Java"; Integer value = courses.get(key); System.out.println(value); } } Output30000 Explanation of the above Program:In the above program, a HashMap is created to store String keys and Integer values.Key-value pairs are added using the put() method.The key "Java" is used to retrieve the value.The get() method is called on the map, passing the key as a parameter.This returns the Integer value mapped to "Java", which is 30000.The value is printed to verify it was retrieved correctly.Note: The get() method returns the value or null if key is not found. Comment More info P pranay0911 Follow Improve Article Tags : Java Java Programs Java-Collections Java-HashMap Java Examples +1 More Explore Java 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 readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 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 Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 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 Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like