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

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
How to choose database in system design interview?
Can I use notes in an Amazon interview?
What questions are asked in the second round of Apple interview?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.