In this example we will see how to java map get value by key . Java HashMap get method returns value of key is present in map if key is not present then it will return null value . There we will discuss different example for map get value by key java .
Example 1 : Java Map get Value By Key – Key is present
In this example we will create HashMap with Integer and String mapping . We add element in map using put method . we use java HashMapget method to get value by key . We will try to get value of key 1 and 4 here , because key is present in HashMap, so java HashMap get method returns corresponding values of key . Let’s see java program for map get value by key java .
import java.util.HashMap;
import java.util.Map;
public class HashMapGetValueBykey {
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,"java");
hm.put(2, "vogue");
hm.put(3,"welcomes");
hm.put(4,"you");
/*
* Returns the value to which the specified
* key is mapped, or null if this map contains
* no mapping for the key.
*/
System.out.println("get value by key: 1 is = "+hm.get(1));
System.out.println("get value by key: 4 is = "+hm.get(4));
}
}
Output:
get value by key: 1 is = java
get value by key: 4 is = you
Example 2 : Java Map get Value By Key – Key is not present
Now we will create map and add values in it. In this example we try to get value of key 5 and 10 . because key 5 and 10 is not present in map, So map get method returns null value . Let’s see java get value from map when key is not present .
import java.util.HashMap;
import java.util.Map;
public class HashMapGetValueBykey1 {
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,"java");
hm.put(2, "vogue");
hm.put(3,"welcomes");
hm.put(4,"you");
/*
* Returns the value to which the specified
* key is mapped, or null if this map contains
* no mapping for the key.
*/
System.out.println("get value by key: 10 is = "+hm.get(10));
System.out.println("get value by key: 5 is = "+hm.get(5));
}
}
Output:
get value by key: 10 is = null
get value by key: 5 is = null
Example 3 : Get value Of key in HashMap Using entrySet
We will see how to get value from hashmap in java . In this example we create HashMap using Integer and Sting mapping . We add element in map using put method . We get entrySet from HashMap using entrySet() method. HashMap entrySet() method return set of Entry . We will iterate this entry set and check key is equal to 3 if key match then print value of key from Entry .
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapGetValueBykey2 {
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,"java");
hm.put(2, "vogue");
hm.put(3,"welcomes");
hm.put(4,"you");
Set<Entry<Integer, String>> entrySet =hm.entrySet();
for (Entry<Integer, String> entry : entrySet) {
if (entry.getKey() == 3) {
System.out.println(" value of key 3 is : "+ entry.getValue());
}
}
}
}
Output:
value of key 3 is : welcomes
In this tutorial we have learned how to get value of key in hashmap . We have given different example of java get value from map . you can see more java HashMap program for practice .