In this method the specified value is to be tested whether, it is mapped with at least one or more keys. It returns 'true' if and only if a specified value does exist into
containsValue(Object value) method of Map Interface in Java.
In this section we will discuss how can containsValue(Object value) method be implemented in Map interface in java.
Syntax
boolean containsValue(Object value)
This method checks whether the underlying map mappings a specific value.
In this method the specified value is to be tested whether, it is mapped with at least one or more keys. It returns 'true' if and only if a specified value does exist into the key-value mappings of the underlying map otherwise 'false'.
Parameter description
value : It takes value from the (key-value) mappings that you want to do test for its presence.
Example of containsValue(Object value) method
In this example we will show you how does containsValue(Object value) method work in Map interface. This example will help you to understand how you can test the presence of a specified value. In the following example we will also show you what does this method return when the underlying map may or may not contains the specified value.
Example :
package devmanuals.com; import java.util.Map; import java.util.HashMap; import java.util.Collection; public class MapContainsValue { public static void main (String args[]){ Map mp=new HashMap(); mp.put(1, "A"); mp.put(2, "B"); mp.put(3, "C"); mp.put(4, "D"); System.out.println("Mapping = "+mp); Collection c= mp.values(); System.out.println("collection of values = "+c); boolean bol= mp.containsValue("D"); System.out.println("Does the value D exist into the collection of values : "+bol); bol= mp.containsValue("E"); System.out.println("Does the value E exist into the collection of values : "+bol); } }
Output :
Mapping = {1=A, 2=B, 3=C, 4=D} collection of values = [A, B, C, D] Does the value D exist into the collection of values : true Does the value E exist into the collection of values : false |
[ 0 ] Comments