Java LinkedHashMap clear() Method

The LinkedHashMap.clear() method in Java is used to remove all key-value pairs from a LinkedHashMap.

Table of Contents

  1. Introduction
  2. clear Method Syntax
  3. Examples
    • Clearing a LinkedHashMap
  4. Real-World Use Case
    • Example: Resetting User Sessions
  5. Conclusion

Introduction

The LinkedHashMap.clear() method is a member of the LinkedHashMap class in Java. It allows you to remove all key-value pairs from the LinkedHashMap, effectively clearing the map and leaving it empty. This can be useful in scenarios where you need to reset the map or release resources.

clear() Method Syntax

The syntax for the clear method is as follows:

public void clear()
  • The method does not take any parameters.
  • The method returns void.

Examples

Clearing a LinkedHashMap

The clear method can be used to remove all entries from a LinkedHashMap.

Example

import java.util.LinkedHashMap;

public class ClearExample {
    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 LinkedHashMap before clearing
        System.out.println("LinkedHashMap before clear: " + people);

        // Clearing the LinkedHashMap
        people.clear();

        // Printing the LinkedHashMap after clearing
        System.out.println("LinkedHashMap after clear: " + people);
    }
}

Output:

LinkedHashMap before clear: {Ravi=25, Priya=30, Vijay=35}
LinkedHashMap after clear: {}

Real-World Use Case

Example: Resetting User Sessions

A common real-world use case for LinkedHashMap.clear() is resetting user sessions in an application. For example, let's consider a simple web application where user sessions are tracked, and we want to clear all sessions at the end of the day.

Example

import java.util.LinkedHashMap;
import java.util.Map;

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

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

        // Printing user sessions before clearing
        System.out.println("User Sessions before clear: ");
        for (Map.Entry<String, String> entry : userSessions.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Clearing all user sessions
        userSessions.clear();

        // Printing user sessions after clearing
        System.out.println("User Sessions after clear: " + userSessions);
    }
}

Output:

User Sessions before clear:
Ravi: Active
Priya: Inactive
Vijay: Active
User Sessions after clear: {}

In this example, LinkedHashMap.clear() is used to remove all user sessions, effectively resetting the session tracker.

Conclusion

The LinkedHashMap.clear() method in Java provides a way to remove all key-value pairs from a LinkedHashMap. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications, especially in scenarios where you need to reset or clear the map. The method allows you to quickly empty the map, making it a versatile tool for data management.

Comments