A hash table is a collection of key-value pair. The isEmpty
method of the Hashtable
class is used to check if the specified Hashtable
object is empty, i.e., it has no key-value entry present.
public boolean isEmpty()
This method doesn’t take any argument.
This method returns true
if the Hashtable
object doesn’t have any mappings. Otherwise, false
will be returned.
The following example shows how to use the isEmpty
method.
import java.util.Hashtable;class HashtableisEmptyExample {public static void main( String args[] ) {Hashtable<Integer, String> map = new Hashtable<>();System.out.println("The map is " + map);System.out.println("Checking if Hashtable is empty : "+ map.isEmpty());map.put(1, "one");map.put(2, "two");System.out.println("\nAfter Adding some mappings to the map is " + map);System.out.println("Checking if Hashtable is empty : "+ map.isEmpty());}}
In the code above:
In line 1, we imported the Hashtable
class.
In line 4, we created a Hashtable
object with the name map
.
In line 6, we used the isEmpty
method to check if the map
is empty. true
is returned as a result because the map
object is empty.
In lines 7 and 8, we used the put
method to add two mappings ({1=one, 2=two}
) to the map
object.
In line 10, we used the isEmpty
method to check if the map
is empty. false
is returned as a result because the map
object is not empty.