How to check if a specific key is present in a Hashtable or not?

How to Check if a Specific Key is Present in a Hashtable in Java

In Java, you can use the Hashtable class from the java.util package to store key-value pairs. To check if a specific key is present in a Hashtable, you can use the containsKey() method. This method returns true if the key is present in the hashtable and false otherwise.

Example Implementation

Here’s an example to demonstrate how to check if a specific key is present in a Hashtable:

import java.util.Hashtable; public class Main { public static void main(String[] args) { // Creating a Hashtable Hashtable<String, Integer> hashtable = new Hashtable<>(); // Adding key-value pairs to the Hashtable hashtable.put("One", 1); hashtable.put("Two", 2); hashtable.put("Three", 3); // Checking if a specific key is present String keyToCheck = "Two"; if (hashtable.containsKey(keyToCheck)) { System.out.println("Key \"" + keyToCheck + "\" is present in the hashtable."); } else { System.out.println("Key \"" + keyToCheck + "\" is not present in the hashtable."); } // Checking for a key that is not present keyToCheck = "Four"; if (hashtable.containsKey(keyToCheck)) { System.out.println("Key \"" + keyToCheck + "\" is present in the hashtable."); } else { System.out.println("Key \"" + keyToCheck + "\" is not present in the hashtable."); } } }

Explanation

  1. Creating a Hashtable:

    • We create an instance of Hashtable to store key-value pairs.
  2. Adding Key-Value Pairs:

    • We use the put() method to add key-value pairs to the hashtable.
  3. Checking for Key Presence:

    • We use the containsKey() method to check if a specific key is present in the hashtable.
    • If the key is present, containsKey() returns true; otherwise, it returns false.

Output

Key "Two" is present in the hashtable.
Key "Four" is not present in the hashtable.

Summary

  • Hashtable: A data structure that stores key-value pairs.
  • containsKey() Method: Used to check if a specific key is present in the hashtable.
  • Usage:
    • Returns true if the key is present.
    • Returns false if the key is not present.

This method is straightforward and efficient for checking the presence of keys in a Hashtable. For more in-depth knowledge and practical examples on Java data structures and other programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive courses on essential coding and interview techniques.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Gaining confidence through frequent mock system design sessions
Will Coinbase do layoffs?
Documenting personal learning curves to track interview readiness
Is it difficult to get hired at Amazon?
Relying on established best practices for given tech stacks
Refinement techniques for partial solutions under interview time limits
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.