How to Check if HashTable Contains Specified Key in Java


This example will use containsKey method of Hashtable class to check if Hashtable object contains a particular key.

Source Code

package com.beginner.examples;

import java.util.Hashtable;

public class CheckKeyOfHashTableExample {

	public static void main(String[] args) {
		// Create a HashTable object
		Hashtable hTable = new Hashtable();

		// Put Key-value
		hTable.put("1", "January");
		hTable.put("2", "February");
		hTable.put("3", "March");

		// To check that the HashTable contains the specified value, use the
		// containsKey() method
		boolean b1 = hTable.containsKey("2");
		boolean b2 = hTable.containsKey("6");

		System.out.println("Key 2:" + b1 + "nKey 6:" + b2);

	}

}

Output:

Key 2:true
Key 6:false

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments