How to Search Value in HashMap Java?

HashMap.containsValue() method is used to check whether a particular value exists or not in HashMap. Its method return true if there is exists value in hashmap.

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

import java.util.HashMap;
import java.util.Map;
 
public class HashMapSearchValue {

public static void main(String[] args) {

Map<Integer,String> hm = new HashMap<Integer,String>();

/*
* Associates the specified value with the specified key in
this map (optional operation). If the map previously
contained a mapping for the key, the old value is
replaced by the specified value
*/
hm.put(1,"ankush");
hm.put(2, "amit");
hm.put(3,"shivam");
hm.put(4,"ankit");
hm.put(5, "yogesh");

System.out.println(" value john ="+hm.containsValue("john"));
System.out.println(" value amit ="+hm.containsValue("amit"));

System.out.println(" value piyush ="+hm.containsValue("piyush"));
System.out.println(" value shivam ="+hm.containsValue("shivam"));

}

}

Output :

value john =false

value amit =true

value piyush =false

value shivam =true

 

Java Doc – HashMap

 

Leave a Reply

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

41 − = 35