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
Cross-referencing official company interview guidelines
What does xrange() function do in Python?
How to explain data modeling in interviews?
How to get strong in DSA?
How do I start DSA for beginners?
Does Datadog hire in Canada?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

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

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
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

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