Write a Java program to test if a map contains a mapping for the specified key


The program then checks whether the key 'Pink' exists in the HashMap using the containsKey() method. Since 'Pink' does exist as a key in the HashMap, the program outputs "yes!" followed by the corresponding value associated with the key using the get() method.

The program then checks whether the key 'Green' exists in the HashMap using the containsKey() method. Since 'Green' does not exist as a key in the HashMap, the program outputs "no!".

Source Code

import java.util.*;  
public class Map_Contains
{  
	public static void main(String args[])
	{
		HashMap <String, Integer> hash_map = new HashMap <String, Integer> ();
		hash_map.put("Red", 1);
		hash_map.put("Pink", 2);
		hash_map.put("Black", 3);
		hash_map.put("White", 4);
		hash_map.put("Blue", 5);
 
		System.out.println("HashMap : " + hash_map);
		System.out.println("1. Is key 'Pink' Exists ?");
		if (hash_map.containsKey("Pink"))
		{			
			System.out.println("yes! - " + hash_map.get("Pink")); //key exists	
		}
		else
		{			
			System.out.println("No!");//key does not exists
		}
 
		System.out.println("\n2. Is key 'Green' Exists ?");
		if (hash_map.containsKey("orange"))
		{
			System.out.println("yes! - " + hash_map.get("Green"));
		} 
		else
		{
			System.out.println("no!");
		}
	}
}

Output

HashMap : {Red=1, Pink=2, White=4, Blue=5, Black=3}
1. Is key 'Pink' Exists ?
yes! - 2

2. Is key 'Green' Exists ?
no!