TreeMap containsValue(Object) in Java

containsValue(Object): This method is available in java.util.TreeMap class of Java.

Syntax:

boolean java.util.TreeMap.containsValue(Object value)

This method takes one argument. This method returns true if this map maps one or more keys to the specified value.

Parameters: One parameter is required for this method.

value: value whose presence in this map is to be tested.

Returns: true if a mapping to value exists; false otherwise.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapcontainsValue {
    public static void main(String[] args) {

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        String value = "Hello";

        System.out.println(treeMap.containsValue(value));
    }
}

Output:

true


No comments:

Post a Comment