How to Check If HashMap Contains a Particular Value in Java


This example aims to demonstrate how to check if HashMap object contains a particular value by the containsValue method in Java.

2 .Codes

package com.beginner.examples;

import java.util.HashMap;

public class CheckHashMapContainsValueExample {

	public static void main(String[] args) {
		// Create a HashMap object
		HashMap map = new HashMap();

		// Put Key-value
		map.put("1", "Jan");
		map.put("2", "Feb");
		map.put("3", "Mar");
		map.put("4", "Apr");
		map.put("5", "May");

		// containsValue() the method Returns True if one or more exist,
		// or False if none exists
		boolean b1 = map.containsValue("Mar");
		boolean b2 = map.containsValue("Jun");
		
		System.out.println("Mar:"+b1+"nJun:"+b2);

	}

}

Output:

Mar:true
Jun:false

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments