Hashmap was first introduced in Java 1.2.The Hashmap class is included in the java.util package.The data in a hashmap is stored in two parts: the key and the value, which can be accessed using an index of another type.A hashmap have unique keys , inserting a duplicate key will replace the data of the key. Hashmap class have many inbuilt method, one of those method is Java Hashmap isEmpty() Method.
To know more about Hashmap isEmpty() Method read the complete article
Java Hashmap isEmpty() Method
The hashmap isEmpty() method checks if the hashmap have the key/value mapping int the hashmap or not. In other words it checks if the hashmap is empty or not. Below in this page you can find it’s syntax, return values, parameters with detailed examples.
Syntax :
hashmap.isEmpty();
Parameters :
This method does not take any parameters.
Return values :
True : If there is no key/value mapping or the hashmap is empty.
False : If hashmap have key/value mapping or the hashmap is not empty.
import java.util.*;
public class Main
{
public static void main (String[]args)
{
// Creating an empty HashMap
HashMap < Integer, String > hash_map_1 =
new HashMap < Integer, String > ();
// Mapping string values to int keys
hash_map_1.put (10, "Java");
hash_map_1.put (20, "Hashmap");
hash_map_1.put (25, "containsValue");
hash_map_1.put (30, "method");
// Creating an empty HashMap
HashMap < Integer, String > hash_map_2 =
new HashMap < Integer, String > ();
//checking if the hash_map_1 is empty or not
System.out.println ("hash_map_1 is empty : " + hash_map_1.isEmpty ());
//checking if the hash_map_2 is empty or not
System.out.println ("hash_map_2 is empty : " + hash_map_2.isEmpty ());
}
}
Output :
hash_map_1 is empty : false
hash_map_2 is empty : true
What happened above:In the above example two hashmaps are created and elements are inserted in hash_map_1. When the hash_map_1.isEmpty() method is called it returns 'false' because hash_map_1 contains some key/value mepping, while when hash_map_2.isEmpty is called it returns 'true' because hash_map_2 does not contain any key/value mapping.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment