How to Retrieve a Specific Value From a HashMap Using Key? Last Updated : 09 Feb, 2024 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 infoAdvertise with us Next Article How to Retrieve a Specific Value From a HashMap Using Key? P pranay0911 Follow Improve Article Tags : Java Java Programs Java-Collections Java-HashMap Java Examples +1 More Practice Tags : JavaJava-Collections Similar Reads Java Program to Sort a HashMap by Keys and Values HashMap<K, V> is a Java Collection and is a part of java.util package. It provides the basic implementation of the Map interface of Java. It stores the data in the form of Key, Value pairs, where the keys must be unique but there is no restriction for values. If we try to insert the duplicate 3 min read How to Get TreeMap Key or Value using Index in Java? The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using whic 5 min read How to Get a Value From LinkedHashMap by Index in Java? LinkedHashMap is a predefined class in Java which is similar to HashMap, contain key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to get value from LinkedHashMap by their Index in other words, an order of their insertion. As an advantage of Link 4 min read How to Convert Two Arrays Containing Keys and Values to HashMap in Java? HashMap is part of the Collections framework of java. It stores the data in the form of key-value pairs. These values of the HashMap can be accessed by using their respective keys or the key-value pairs can be accessed using their indexes (of Integer type). HashMap is similar to Hashtable in java. T 2 min read How to Implement a Custom Hash function for Keys in a HashMap in Java? In Java, HashMap is the data structure that implements the Map interface. This is used to save the data in the form of key-value pairs. In this article, we will learn how to implement a Custom Hash function for keys in a HashMap in Java. In Java, implementing the custom hash function for keys in a H 2 min read Like