Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




MAP - EMPTY() METHOD


The empty() Method is used to check if a Map is empty or not.


Let us say, we have a Map that contains three entries. And we want to check if the Map is empty or not.


We can use the front() Method to achieve the above.


Example :



#include <iostream>
#include <map>

using namespace std;

int main() {

    map<string, string> x = 
        {
	        { "Five", "Is a Number" }, 
	        { "John", "Is a Name" }, 
	        { "C++", "Is a Language"}
        };
        
    if (x.empty()) {
    	cout << "The Map is empty"; 
    }	
    else {
    	cout << "The Map is not empty"; 
    } 
    
    return 0;    
}


Output :



  The Map is not empty

So, in the above code we have created a Map and initialised to the variable x.


map<string, string> x =
	{
		{ "Five", "Is a Number" },
		{ "John", "Is a Name" },
		{ "C++", "Is a Language"}
	};

Below is how the values are positioned in the Map,

java_Collections

Next, we have used the empty() method to check if the Map is empty or not.


if (x.empty())

In this case the Map is not empty.


if (x.empty()) {
	cout << "The Map is empty";
}
else {
	cout << "The Map is not empty";
}

So, we got the output as,

Output :



  The Map is not empty