What is the TreeMap.containsValue() method in Java?

In this shot, we will learn how to use the TreeMap.containsValue() method in Java, which is present in the TreeMap class inside the java.util package.

The TreeMap.containsValue() method is used to check whether a particular value is being mapped by any of the keys present in the TreeMap.

Syntax

The syntax of the TreeMap.containsValue() method is shown below:

boolean containsValue(Object value)

Here, the value is of the type Object, as every class is a subclass of the Object class.

Parameter

The TreeMap.contains value() method accepts only one parameter, i.e., the value which needs to be checked to see whether it is mapped with any keys in the TreeMap or not.

Return value

The TreeMap.containsValue() returns a boolean value:

  • True: If the value is mapped with any key present in the TreeMap.
  • False: If the value is not mapped with any key present in the TreeMap.

Code

This method can be applied to any mapping with combinations of different data types.

Let’s have a look at some examples.

Example 1: <Integer, String>

In this example, we map string values to integer keys. However, this method can be applied to any mapping with combinations of different data types.

// importing required package
import java.util.*;
// Main class
class Main
{
// main driver function
public static void main(String[] args)
{
// creating an empty TreeMap
TreeMap<Integer, String> t1 = new TreeMap<Integer, String>();
// inserting values in the TreeMap
t1.put(1, "Educative");
t1.put(5, "Python");
t1.put(2, "Java");
t1.put(27, "Learn Coding");
t1.put(9, "C++");
// checking for values in the TreeMap
System.out.println("Value = Python in TreeMap? " + t1.containsValue("Python"));
System.out.println("Value = Javascript in TreeMap? " + t1.containsValue("Javascript"));
System.out.println("Value = C++ in TreeMap? " + t1.containsValue("C++"));
}
}

Explanation:

  • In line 2, we imported the required package.
  • In line 5, we made a Main class.
  • In line 8, we made a main() function.
  • In line 11, we declared a TreeMap consisting of keys of type Integer and values of type String.
  • From lines 14 to 18, we inserted values in the TreeMap by using the TreeMap.put() method.
  • From line 21 to 23, we displayed whether the value passed as argument is mapped with any keys present in the TreeMap, or not, with a message.

Example 2: <String, Integer>

In this example, we map integer values to string keys.

// importing required package
import java.util.*;
// Main class
class Main
{
// main driver function
public static void main(String[] args)
{
// creating an empty TreeMap
TreeMap<String, Integer> t1 = new TreeMap<String, Integer>();
// inserting values in the TreeMap
t1.put("Educative", 1);
t1.put("Python", 2);
t1.put("Java", 3);
t1.put("Learn Coding", 4);
t1.put("C++", 5);
// checking for values in the TreeMap
System.out.println("Value = 1 in TreeMap? " + t1.containsValue(1));
System.out.println("Value = 10 in TreeMap? " + t1.containsValue(10));
System.out.println("Value = 3 in TreeMap? " + t1.containsValue(3));
}
}

Explanation:

  • In line 2, we imported the required package.
  • In line 5, we made a Main class.
  • In line 8, we made a main() function.
  • In line 11, we declared a TreeMap consisting of keys of type String and values of type Integer.
  • From lines 14 to 18, we inserted values in the TreeMap by using the TreeMap.put() method.
  • From line 21 to 23, we displayed whether the value passed as argument is mapped with any keys present in the TreeMap, or not, with a message.

So, that is how to use the TreeMap.containsValue() method in Java to check if a particular value is being mapped by any of the keys present in the TreeMap, or not.

Free Resources