Java TreeMap containsKey() Method

The TreeMap.containsKey() method in Java is used to check if a specified key 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.containsKey() can be used effectively.

Table of Contents

  1. Introduction
  2. containsKey Method Syntax
  3. Examples
    • Checking for the Presence of a Key in a TreeMap
    • Handling Non-Existent Keys
  4. Real-World Use Case
    • Example: Checking for Student Names
  5. Conclusion

Introduction

The TreeMap.containsKey() method is a member of the TreeMap class in Java. It allows you to check if a specified key is present in the map. This method returns true if the map contains a mapping for the specified key, and false otherwise.

containsKey() Method Syntax

The syntax for the containsKey method is as follows:

public boolean containsKey(Object key)
  • Parameters:
    • key: The key whose presence in the map is to be tested.
  • Returns: true if the map contains a mapping for the specified key, false otherwise.

Examples

Checking for the Presence of a Key in a TreeMap

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

Example

import java.util.TreeMap;

public class ContainsKeyExample {
    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 key "Priya" is present in the TreeMap
        boolean containsKey = treeMap.containsKey("Priya");

        // Printing the result
        System.out.println("TreeMap contains key 'Priya': " + containsKey);
    }
}

Output:

TreeMap contains key 'Priya': true

Handling Non-Existent Keys

If the key is not present in the TreeMap, the containsKey method returns false.

Example

import java.util.TreeMap;

public class NonExistentKeyExample {
    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 key "Amit" is present in the TreeMap
        boolean containsKey = treeMap.containsKey("Amit");

        // Printing the result
        System.out.println("TreeMap contains key 'Amit': " + containsKey);
    }
}

Output:

TreeMap contains key 'Amit': false

Real-World Use Case

Example: Checking for Student Names

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

Example

import java.util.TreeMap;

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

        // Adding student names and their IDs to the TreeMap
        studentNames.put("Ravi", 101);
        studentNames.put("Priya", 102);
        studentNames.put("Vijay", 103);
        studentNames.put("Anita", 104);
        studentNames.put("Suresh", 105);

        // Checking if the student name "Priya" is present in the TreeMap
        boolean containsName = studentNames.containsKey("Priya");

        // Printing the result
        System.out.println("TreeMap contains student name 'Priya': " + containsName);
    }
}

Output:

TreeMap contains student name 'Priya': true

In this example, TreeMap.containsKey() is used to check if a particular student name is present in a map of student names and their IDs, making it easy to determine if a specific student is enrolled.

Conclusion

The TreeMap.containsKey() method in Java provides a way to check if a specified key 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 key 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