containsValue(Object): This method is available in java.util.Hashtable class of Java.
Syntax:
boolean java.util.Hashtable.containsValue(Object value)
This method takes one argument. This method returns true if this hashtable maps one or more keys to this value.
Parameters: One parameter is required for this method.
value: value whose presence in this hashtable is to be tested.
Returns: true if this map maps one or more keys to the specified value.
Throws:
NullPointerException - if the value is null.
Approach 1: When no exception
Java
import java.util.Hashtable;public class HashtablecontainsValue {public static void main(String[] args) {Hashtable<String, Integer> hashtable =new Hashtable<String, Integer>();hashtable.put("Hello", 1);hashtable.put("World", 2);Integer value = 1;System.out.println(hashtable.containsValue(value));}}
Output:
true
Approach 2: NullPointerException
Java
import java.util.Hashtable;public class HashtablecontainsValue {public static void main(String[] args) {Hashtable<String, Integer> hashtable =new Hashtable<String, Integer>();hashtable.put("Hello", 1);hashtable.put("World", 2);Integer value = null;System.out.println(hashtable.containsValue(value));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Hashtable.contains(Hashtable.java:311) at java.base/java.util.Hashtable.containsValue(Hashtable.java:338)
No comments:
Post a Comment