Java TreeMap containsValue(Object value) Method Example

The containsValue(Object value) method of java.util.TreeMap class returns true if this treemap maps one or more keys to this value.

Signature

public boolean containsValue(Object value)

This method returns true if this treemap maps one or more keys to this value, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)).

Example

The following example shows the usage of java.util.TreeMap.containsValue(value) method.

import java.util.TreeMap;

public class ContainsValueMethodTreeMapExample 
{
    public static void main(String args[])
    {
        // create an empty TreeMap
        TreeMap<Integer,String>  treeMap1 = new TreeMap<Integer,String>();
        
        // use put() method to put elements to the TreeMap
        treeMap1.put(1,"Element1");
        treeMap1.put(2,"Element2");
        treeMap1.put(3,"Element3");
        treeMap1.put(4,"Element4");
        treeMap1.put(5,"Element5");
        
        //Check if the treeMap1 contains Value "Element2"
        System.out.println("Does \"treeMap1\" contains value \"Element2\" : "+treeMap1.containsValue("Element2"));
    }
}

Output

Does "treeMap1" contains values "Element2" : true

Leave a Reply

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