Write a Java program to check whether a HashMap contains a specified value or not


The Java program creates a HashMap named stu with integer keys and string values. It then checks whether the value "Joes" is present in the HashMap using the containsValue method. If the value is present, it prints "Value 'Joes' is contained in 'stu' HashMap", otherwise, it prints "Value 'Joes' is not contained in 'stu' HashMap".

Source Code

import java.util.*;
public class Specified_ValueNot
{
	public static void main(String[] args)
	{
		HashMap <Integer,String> stu = new HashMap <>();
		stu.put(101, "Kim");
		stu.put(102, "Arun");
		stu.put(103, "Deepika");
		stu.put(105, "Joes");
		stu.put(107, "John");
		stu.put(108, "Vijay");
 
		if (stu.containsValue("Joes"))
		{
			System.out.println("Value 'Joes' is contained in 'stu' HashMap");
		}
		else
		{
			System.out.println("Value 'Joes' is not contained in 'stu' HashMap");
		}
	}
}

Output

Value 'Joes' is contained in 'stu' HashMap