Java TreeMap size() Method

The TreeMap.size() method in Java is used to determine the number of key-value pairs contained in a TreeMap. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.size() can be used effectively.

Table of Contents

  1. Introduction
  2. size Method Syntax
  3. Examples
    • Getting the Size of a TreeMap
    • After Adding and Removing Entries
  4. Real-World Use Case
    • Example: Checking the Number of Contacts
  5. Conclusion

Introduction

The TreeMap.size() method is a member of the TreeMap class in Java. It returns the number of key-value pairs currently stored in the TreeMap. This method is useful for determining the current size of the map, which can help in various scenarios such as conditionally processing data or verifying the map’s contents.

size() Method Syntax

The syntax for the size method is as follows:

public int size()
  • The method does not take any parameters.
  • The method returns an int representing the number of key-value pairs in the TreeMap.

Examples

Getting the Size of a TreeMap

The size method can be used to get the current number of key-value pairs in a TreeMap.

Example

import java.util.TreeMap;

public class SizeExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);

        // Getting the size of the TreeMap
        int size = treeMap.size();

        // Printing the size of the TreeMap
        System.out.println("Size of TreeMap: " + size);
    }
}

Output:

Size of TreeMap: 3

After Adding and Removing Entries

The size method can be used to get the number of entries in the TreeMap after adding and removing key-value pairs.

Example

import java.util.TreeMap;

public class AddRemoveSizeExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);

        // Getting the size of the TreeMap after adding entries
        int initialSize = treeMap.size();
        System.out.println("Initial size of TreeMap: " + initialSize);

        // Removing an entry from the TreeMap
        treeMap.remove("Priya");

        // Getting the size of the TreeMap after removing an entry
        int finalSize = treeMap.size();
        System.out.println("Final size of TreeMap: " + finalSize);
    }
}

Output:

Initial size of TreeMap: 3
Final size of TreeMap: 2

Real-World Use Case

Example: Checking the Number of Contacts

A common real-world use case for TreeMap.size() is managing a contact list where you need to know the number of contacts.

Example

import java.util.TreeMap;
import java.util.Map;

public class ContactManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage contacts
        TreeMap<String, Integer> contacts = new TreeMap<>();

        // Adding contacts to the TreeMap
        contacts.put("Ravi", 1234567890);
        contacts.put("Priya", 9876543210);
        contacts.put("Vijay", 5556667777);
        contacts.put("Anita", 4445556666);

        // Getting the number of contacts
        int numberOfContacts = contacts.size();

        // Printing the number of contacts
        System.out.println("Number of contacts: " + numberOfContacts);

        // Printing the contacts
        System.out.println("Contacts: ");
        for (Map.Entry<String, Integer> entry : contacts.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Number of contacts: 4
Contacts:
Anita: 4445556666
Priya: 9876543210
Ravi: 1234567890
Vijay: 5556667777

In this example, TreeMap.size() is used to determine the number of contacts in a contact list, making it easy to manage and verify the list.

Conclusion

The TreeMap.size() method in Java provides a way to get the number of key-value pairs in a TreeMap. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications. The method allows you to determine the size of the map, which is useful for various data management and verification tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top