In this article, we will discuss how to check whether a value is present in the invoking HashMap or Not ?
1. Searching a value from HashMap :
- Method signature : boolean containsValue(Object value)
- This method is used to search specified value from invoking Map object;
- it can be HashMap or LinkedHashMap or TreeMap
- Returns true, if value is present;
- otherwise return false
- Note: Same example can be used to search for any particular value in LinkedHashMap and TreeMap
SearchSpecifiedValueFromHashMap.java
package in.bench.resources.java.map;
import java.util.HashMap;
public class SearchSpecifiedValueFromHashMap {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
HashMap<String, String> hashMap = new HashMap<String, String>();
// adding key-value pairs to HashMap object
hashMap.put("Google", "Sundar Pichai");
hashMap.put("Facebook", "Mark Zuckerberg");
hashMap.put("LinkedIn", "Reid Hoffman");
hashMap.put("Apple", "Steve Jobs");
hashMap.put("Microsoft", "Bill Gates");
// printing all Key-Value pairs
System.out.println("all Key-Value pairs:\n\n" + hashMap);
// search for value
boolean searchValue = hashMap.containsValue("Bill Gates");
// print to console - searchValue
System.out.println("\n\nWhether value 'Bill Gates' is present"
+ " in hashMap ? "
+ searchValue);
// print to console
System.out.println("\n\nWhether value 'Steve Jobs' is present"
+ " in hashMap ? "
+ hashMap.containsValue("Steve Jobs"));
// print to console
System.out.println("\n\nWhether key 'Shiv Nadar' is present"
+ " in hashMap ? "
+ hashMap.containsValue("Shiv Nadar"));
}
}
Output:
all Key-Value pairs:
{Google=Sundar Pichai, LinkedIn=Reid Hoffman, Apple=Steve Jobs,
Microsoft=Bill Gates, Facebook=Mark Zuckerberg}
Whether value 'Bill Gates' is present in hashMap ? true
Whether value 'Steve Jobs' is present in hashMap ? true
Whether key 'Shiv Nadar' is present in hashMap ? false
Related Articles:
- How to get all keys of a HashMap
- How to get all values of a HashMap
- How to get all Entries or Key-Value pairs of HashMap
- How to get size or length of HashMap
- How to check whether a particular key is present in HashMap
- How to check whether a particular value is present in HashMap
- How to check whether HashMap is empty or not ?
- Adding one HashMap to another HashMap using putAll method
- How to delete an entry of HashMap
- How to delete all entries of HashMap
- How to remove an entry from HashMap by comparing values in Java 8
- How to remove an entry from HashMap by comparing keys in Java 8
- Various ways to iterate through HashMap
- To reverse order the LinkedHashMap contents
- Map: How ConcurrentModificationException can be handled in Java
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Happy Coding !!
Happy Learning !!