Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Check if the TreeMap is Empty

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: 

  1. Using the isEmpty() method
  2. Using the size() method

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 package
import 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);
      
    }
}


Output

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 package
import 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));
    }
}


Output

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.

RELATED ARTICLES

Most Popular

Recent Comments