HashMap containsValue Method in Java

Hashmap containsValue  method is used to check whether a particular value exists or not in java HashMap. Its method return true if there is exists value present in hashmap otherwise false.

Syntax :

public boolean containsValue(Object value) {

}

Parameters:  It takes value as augment whose presence in this map is to be tested
Return Value : It return true if this map maps one or more keys to the specified value

Example 1 : Java Hashmap containsValue() method -Value present

In this example we will  initialize hashmap hm with mapping Integer and String .We check the values “vogue” and “welcomes” are present  or not using map containsvalue() method . Because values are present in hashmap , so hashmap containsvalue() method returns true .

import java.util.HashMap;
import java.util.Map;

public class HashMapContainsValue {
    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");

	System.out.println("is value 'vogue' present  = "+hm.containsValue("vogue"));
	System.out.println("is value 'welcomes' present = "+hm.containsValue("welcomes"));
		
	}
}

Output:

is value 'vogue' present  = true
is value 'welcomes' present = true

Example 2 : Java Hashmap containsValue() method -Value not present

In this example we will create  hashmap hm with mapping Integer and String . Now we will check value presentation using java hashmap.containsValue() method .Here we check value “php” and “welcome ” are present or not in map . Because these value are not present in hashmap  , so it returns false .

import java.util.HashMap;
import java.util.Map;

public class HashMapContainsValue1 {

	    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");

	System.out.println("is value 'php' present  = "+hm.containsValue("php"));
	System.out.println("is value 'welcome' present = "+hm.containsValue("welcome"));
			
		}

	}

Output:

is value 'php' present  = false
is value 'welcome' present = false

Example 3 : Hashmap containsValue() method – Mapping Integer to String keys

We will  initialize hashmap hm with mapping String and Integer . We check value is present or not in hasmap using map Hashmap.containsValue()  method. Here we check value 2,3,9 and “vogue” are present or not in map. Map containsvalue() method returns true for values 2 and 3 and it returns false for value 9 and “vogue”.

import java.util.HashMap;
import java.util.Map;

public class HashMapContainsValue2 {
	
	public static void main(String[] args) {
		Map<String,Integer> hm = new HashMap<String,Integer>();

		hm.put("java",1);
		hm.put("vogue",2);
		hm.put("welcomes",3);
		hm.put("you",4);

	System.out.println("is value '2' present  = "+hm.containsValue(2));
	System.out.println("is value '3' present = "+hm.containsValue(3));
	System.out.println("is value '9' present = "+hm.containsValue(9));
	System.out.println("is value 'vogue' present = "+hm.containsValue("vogue"));
        }
	}

Output:

is value '2' present  = true
is value '3' present = true
is value '9' present = false
is value 'vogue' present = false

In this tutorial we learned how to use java hashmap.containsValue() method . Map containsvalue method is useful when we have to check any particular value is present in map or not . You can see more hashmap program for practice .

Java Doc – HashMap

Leave a Reply

Your email address will not be published. Required fields are marked *

8 + 1 =