Java Hashmap get() Method
What is a Hashmap?
Hashmap was first introduced in Java 1.2.The Hashmap class is included in the java.util package.The data in a hashmap is stored in two parts: the key and the value, which can be accessed using an index of another type.A hashmap have unique keys , inserting a duplicate key will replace the data of the key.
Hashmap class have many inbuilt method, one of those method is hashmap get(). To know more about Java Hashmap get() Method read the complete article
Java Hashmap get() Method
The hashmap get() method is used to retrieve the value mapped to the specified key. If nothing is mapped to the key the method will return the NULL . Below in this page you can find it’s syntax, return values, parameters with detailed examples.
Syntax :
hashmap.get(Object key);
Parameters :
The method takes one parameter Oject key whose mapped value is to be retrieved.
Return values :
Returs the value which is mapped to the given key. If no value is mapped to the key it return NULL.
Hashmap get() Method Examples
Example :
import java.util.*; public class Main { public static void main (String[]args) { // Creating an empty HashMap HashMap < Integer, String > hash_map = new HashMap < Integer, String > (); // Mapping string values to int keys hash_map.put (10, "Java"); hash_map.put (20, "Hashmap"); hash_map.put (25, "clear"); hash_map.put (30, "method"); //retrieving the value mapped to key 20 System.out.println ("Value at key 20 : " + hash_map.get (20)); // retrieving the value mapped to key 5 System.out.println ("Value at key 5 :" + hash_map.get (5)); } }
Output :
Value at key 20 : Hashmap Value at key 5 :null
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment