Java TreeMap containsValue() Method

The TreeMap.containsValue() method in Java is used to check if a specified value is present in the map. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.containsValue() can be used effectively.

Table of Contents

  1. Introduction
  2. containsValue Method Syntax
  3. Examples
    • Checking for the Presence of a Value in a TreeMap
    • Handling Non-Existent Values
  4. Real-World Use Case
    • Example: Checking for Student Ages
  5. Conclusion

Introduction

The TreeMap.containsValue() method is a member of the TreeMap class in Java. It allows you to check if a specified value is present in the map. This method returns true if the map contains one or more keys mapped to the specified value, and false otherwise.

containsValue() Method Syntax

The syntax for the containsValue method is as follows:

public boolean containsValue(Object value)
  • Parameters:
    • value: The value whose presence in the map is to be tested.
  • Returns: true if the map contains one or more keys mapped to the specified value, false otherwise.

Examples

Checking for the Presence of a Value in a TreeMap

The containsValue method can be used to check if a specific value is present in the TreeMap.

Example

import java.util.TreeMap;

public class ContainsValueExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Checking if the value 30 is present in the TreeMap
        boolean containsValue = treeMap.containsValue(30);

        // Printing the result
        System.out.println("TreeMap contains value 30: " + containsValue);
    }
}

Output:

TreeMap contains value 30: true

Handling Non-Existent Values

If the value is not present in the TreeMap, the containsValue method returns false.

Example

import java.util.TreeMap;

public class NonExistentValueExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Checking if the value 50 is present in the TreeMap
        boolean containsValue = treeMap.containsValue(50);

        // Printing the result
        System.out.println("TreeMap contains value 50: " + containsValue);
    }
}

Output:

TreeMap contains value 50: false

Real-World Use Case

Example: Checking for Student Ages

A common real-world use case for TreeMap.containsValue() is checking if a particular age is present in a map of student names and their ages.

Example

import java.util.TreeMap;

public class StudentAgesManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage student names and their ages
        TreeMap<String, Integer> studentAges = new TreeMap<>();

        // Adding student names and their ages to the TreeMap
        studentAges.put("Ravi", 20);
        studentAges.put("Priya", 21);
        studentAges.put("Vijay", 22);
        studentAges.put("Anita", 23);
        studentAges.put("Suresh", 24);

        // Checking if the age 21 is present in the TreeMap
        boolean containsAge = studentAges.containsValue(21);

        // Printing the result
        System.out.println("TreeMap contains age 21: " + containsAge);
    }
}

Output:

TreeMap contains age 21: true

In this example, TreeMap.containsValue() is used to check if a particular age is present in a map of student names and their ages, making it easy to determine if a specific age is represented in the student body.

Conclusion

The TreeMap.containsValue() method in Java provides a way to check if a specified value is present in the map. By understanding how to use this method, you can efficiently manage and navigate collections of key-value pairs in your Java applications. The method allows you to determine if a particular value is present in the map, making it a versatile tool for data management in various scenarios.

Leave a Comment

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

Scroll to Top