How to get value of a particular key in hashmap in java?
Answers
Answered by
1
import java.util.HashMap;
class HashMapDemo{
public static void main(String[] args)
{
// Create a HashMap
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
//add elements to HashMap hmap.put(1, "AA");
hmap.put(2, "BB");
hmap.put(3, "CC");
hmap.put(4, "DD");
// Getting values from HashMap
String val=hmap.get(4);
System.out.println("The Value mapped to Key 4 is:"+ val);
/* Here Key "5" is not mapped to any value so this
* operation returns null.
*/
String val2=hmap.get(5);
System.out.println("The Value mapped to Key 5 is:"+ val2);
}
}
class HashMapDemo{
public static void main(String[] args)
{
// Create a HashMap
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
//add elements to HashMap hmap.put(1, "AA");
hmap.put(2, "BB");
hmap.put(3, "CC");
hmap.put(4, "DD");
// Getting values from HashMap
String val=hmap.get(4);
System.out.println("The Value mapped to Key 4 is:"+ val);
/* Here Key "5" is not mapped to any value so this
* operation returns null.
*/
String val2=hmap.get(5);
System.out.println("The Value mapped to Key 5 is:"+ val2);
}
}
Similar questions