Java Program to Check if the TreeMap is Empty

The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
Approaches:Â
Method 1: Using the isEmpty() method
The java.util.TreeMap.isEmpty() method of TreeMap class is used to check for the emptiness of the TreeMap.
Syntax :
TreeMap.isEmpty()
Parameters: The method does not take any parameters.
Return Value: The method returns boolean true if the TreeMap is empty else false. So, it will return false if there is at least one key-value mapping in the TreeMap object else True.
Example:
Java
// Java Program to Check if the TreeMap is Empty// using the isEmpty() methodÂ
// Importing TreeMap class of// java.util packageimport java.util.TreeMap;Â
public class GFG {Â
    // Main driver method    public static void main(String[] args)    {Â
        // Create an empty TreeMap        TreeMap<Integer, String> tmap            = new TreeMap<Integer, String>();Â
        // Check TreeMap is empty or not        boolean isEmpty = tmap.isEmpty();        System.out.println("Is tmap empty : " + isEmpty);Â
        // Mapping string values to int keys        tmap.put(1, "Geeks");        tmap.put(2, "For");        tmap.put(3, "skeeG");Â
        // Displaying the TreeMap        System.out.println("The Mappings are: " + tmap);Â
        // Checking again TreeMap is empty or not        isEmpty = tmap.isEmpty();Â
        // Display boolean output again        // to show isEmpty() method functionality        System.out.println("Is tmap empty : " + isEmpty);          }} |
Is tmap empty : true
The Mappings are: {1=One, 2=Two}
Is tmap empty : false
Method 2 : Using the size() methodÂ
The java.util.TreeMap.size() method of TreeMap class is used to check for the emptiness of the TreeMap by comparing the size with 0. The method returns True if TreeMap is empty else false.
Syntax :
(TreeMap.size() == 0) ;
Parameters: The method does not take any parameters.
Return Value: The method returns boolean True if the TreeMap is empty else false.
Example:
Java
// Java Program to Check if the TreeMap is Empty// and illustrating the size() methodÂ
// Importing TreeMap class of// java.util packageimport java.util.TreeMap;Â
public class GFG {Â
    // Main driver method    public static void main(String[] args)    {Â
        // Create an empty TreeMap        TreeMap<Integer, String> tmap            = new TreeMap<Integer, String>();Â
        // Check TreeMap is empty or not        // Using size() method        System.out.println("Is map empty : "                           + (tmap.size() == 0));Â
        // Mapping string values to int keys        // Custom inputs mapping        tmap.put(1, "One");        tmap.put(2, "Two");Â
        // Displaying the TreeMap        System.out.println("The Mappings are: " + tmap);Â
        // Display boolean output again        // to show size() method functionality        System.out.println("Is map empty : "                           + (tmap.size() == 0));    }} |
Is map empty : true
The Mappings are: {1=One, 2=Two}
Is map empty : false
Note: The same operation can be performed with any type of Mappings with variation and a combination of different data types.



