Java LinkedHashMap size() Method

The LinkedHashMap.size() method in Java is used to determine the number of key-value mappings in a LinkedHashMap.

Table of Contents

  1. Introduction
  2. size Method Syntax
  3. Examples
    • Determining the Size of a LinkedHashMap
    • After Adding and Removing Entries
  4. Real-World Use Case
    • Example: Tracking the Number of Active Users
  5. Conclusion

Introduction

The LinkedHashMap.size() method is a member of the LinkedHashMap class in Java. It allows you to determine the number of key-value pairs currently stored in the map. This can be useful in scenarios where you need to know the size of the map, such as for validating the map's state or for iteration purposes.

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 the number of key-value pairs in the map as an int.

Examples

Determining the Size of a LinkedHashMap

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

Example

import java.util.LinkedHashMap;

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

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

        // Getting the size of the LinkedHashMap
        int size = people.size();

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

Output:

Size of LinkedHashMap: 3

After Adding and Removing Entries

The size method reflects the current number of key-value pairs in the map, which can change after adding or removing entries.

Example

import java.util.LinkedHashMap;

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

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

        // Printing the initial size of the LinkedHashMap
        System.out.println("Initial size of LinkedHashMap: " + people.size());

        // Removing an entry from the LinkedHashMap
        people.remove("Priya");

        // Printing the size of the LinkedHashMap after removal
        System.out.println("Size of LinkedHashMap after removal: " + people.size());
    }
}

Output:

Initial size of LinkedHashMap: 3
Size of LinkedHashMap after removal: 2

Real-World Use Case

Example: Tracking the Number of Active Users

A common real-world use case for LinkedHashMap.size() is tracking the number of active users in an application. For example, let's consider a scenario where user sessions are stored in a LinkedHashMap, and we need to determine the number of active user sessions.

Example

import java.util.LinkedHashMap;

public class ActiveUserTracker {
    public static void main(String[] args) {
        // Creating a LinkedHashMap to store user sessions
        LinkedHashMap<String, String> userSessions = new LinkedHashMap<>();

        // Adding some user sessions
        userSessions.put("Ravi", "Active");
        userSessions.put("Priya", "Active");
        userSessions.put("Vijay", "Inactive");

        // Getting the number of active user sessions
        int activeUserCount = 0;
        for (String status : userSessions.values()) {
            if (status.equals("Active")) {
                activeUserCount++;
            }
        }

        // Printing the number of active user sessions
        System.out.println("Number of active user sessions: " + activeUserCount);
    }
}

Output:

Number of active user sessions: 2

In this example, LinkedHashMap.size() is used to determine the number of key-value pairs, and additional logic is applied to count only the active sessions, demonstrating how to use the method in conjunction with other operations.

Conclusion

The LinkedHashMap.size() method in Java provides a way to determine the number of key-value pairs in a LinkedHashMap. 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 verify the map's state, making it a versatile tool for data management and validation.

Comments