Java Program to Check if the TreeMap is Empty
Last Updated :
07 Sep, 2021
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:
- Using the isEmpty() method
- 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);
}
}
OutputIs 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));
}
}
OutputIs 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.
Similar Reads
Program to Convert HashMap to TreeMap in Java HashMap is a part of Javaâs collection since Java 1.2. It provides the basic implementation of Map interface of Java which stores the data in (Key, Value) pairs. To access a value in HashMap, one must know its key. HashMap is known as HashMap because it uses a technique Hashing for storage of data.
6 min read
Program to Convert HashMap to TreeMap in Java HashMap is a part of Javaâs collection since Java 1.2. It provides the basic implementation of Map interface of Java which stores the data in (Key, Value) pairs. To access a value in HashMap, one must know its key. HashMap is known as HashMap because it uses a technique Hashing for storage of data.
6 min read
Java Program to Implement TreeMap API The TreeMap is used to implement Map interface and NavigableMap along with the AbstractMap class in java. The map is sorted by the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Now, in implementing TreeMap API, the task is div
6 min read
How to Check if LinkedHashMap is Empty in Java? The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements
2 min read
How to Remove the First Entry or Last Entry from the Java TreeMap? TreeMap is the implementation class of the Map interface. It allows the objects to be sorted according to the keys and sorting can be natural sorting or you can use a comparator. Insertion order is also not preserved in Tree Map. Syntax: TreeMap<String,Integer> map = new TreeMap<>();In t
6 min read
How to Merge Two TreeMaps in Java? In Java, TreeMap is a pre-defined class that implements the NavigableMap interface and extends the AbstractMap class. In this article, we will learn to Merge Two Tree Maps in Java. Program to Merge Two TreeMaps in JavaWe are using the putAll method is inherited from the AbstractMap class in this man
2 min read