Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is the HashTable.isEmpty() method in Java?

Sumit Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In this shot, we will learn about the HashTable.isEmpty() method in Java.

The isEmpty method of HashTable is used to check if there is a key-value pair in the hashtable or not.

Syntax

Hashtable.isEmpty();

Parameters

This method does not accept any parameters.

Return value

The Hashtable.isEmpty(); method returns true if there is a key-value pair present in the hashtable. Otherwise, it returns false.

Code

Let’s have a look at the code.

import java.util.*;
class Solution1 {
public static void main(String args[]) {
// create two hash tables
Hashtable<Integer,String> table1 = new Hashtable<Integer,String>();
Hashtable<Integer,String> table2 = new Hashtable<Integer,String>();
// put values in two tables
table1.put(1, "A");
table1.put(2, "B");
table1.put(3, "C");
table1.put(4, "D");
// display result
System.out.println("is table1 empty:"+table1.isEmpty());
System.out.println("is table2 empty:"+table2.isEmpty());
}
}
Using the HashTable.isEmpty() method in Java

Explanation

  • In line 1, we import the java.util.* classes to include the HashTable data structure.
  • In lines 7 and 8, we initiate the objects table1 and table2.
  • From lines 11 to 14, we insert some key-value pairs in table1.
  • In line 17, we check if table1 is empty or not.
  • In line 18, we check if table2 is empty or not and print the result.

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring