Check if a particular value exists in TreeMap in Java

In this tutorial, we will learn how to Check if a particular value exists in TreeMap in Java ?

TreeMap in Java provides an efficient way of storing key/value pairs in sorted order and allows rapid retrieval of the elements in map. The TreeMap class extends AbstractMap and implements the NavigableMap interface. It creates maps stored in a tree structure. TreeMap is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time. To access a value in TreeMap, you need to have a key.

To check if a particular value exists in TreeMap or not, TreeMap class in Java provides a method containsValue(). We will use this method to check if particular value exists in TreeMap. The syntax of this method is:

Syntax of containsValue() method:

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

Returns:
true if the value exists in map.

Example:

 

Output:

 

Related posts

Leave a Comment