Home » Core Java » util » TreeMap » Check value existence in TreeMap example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Check value existence in TreeMap example

This is an example of how to check a value existence in a TreeMap in Java. Checking if a value exists in a TreeMap implies that you should:

  • Create a new TreeMap.
  • Populate the map with elements, with put(K key, V value) API method of TreeMap.
  • Invoke containsValue(Object value) API method of TreeMap. The method returns true if this map maps one or more keys to the specified value and false otherwise.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
import java.util.TreeMap;
public class CheckValueTreeMap {
  public static void main(String[] args) {
    // Create a TreeMap and populate it with elements
    TreeMap treeMap = new TreeMap();
    // boolean containsValue(Object key) returns true if the value is mapped to one or more keys otherwise false.
    boolean exists = treeMap.containsValue("element_2");
    System.out.println("element_2 exists in TreeMap ? : " + exists);


element_2 exists in TreeMap ? : true

This was an example of how to check a value existence in a TreeMap in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments