🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
HashMap.clear() method in Java is used to remove all mappings from a HashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
clearMethod Syntax- Examples
- Clearing All Entries from a HashMap
- Real-World Use Case: Resetting a User Session Store
- Conclusion
Introduction
The clear() method is a member of the HashMap class in Java. It allows you to remove all key-value mappings from the HashMap, effectively resetting it to an empty state. This can be useful when you need to reuse the same HashMap instance without retaining any of the previous data.
clear() Method Syntax
The syntax for the clear method is as follows:
public void clear()
- The method does not take any parameters.
- The method does not return any value.
Examples
Clearing All Entries from a HashMap
The clear method can be used to remove all entries from a HashMap.
Example
import java.util.HashMap;
public class ClearExample {
public static void main(String[] args) {
// Creating a HashMap with String keys and Integer values
HashMap<String, Integer> people = new HashMap<>();
// Adding entries to the HashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Printing the HashMap before clearing
System.out.println("HashMap before clear: " + people);
// Clearing all entries from the HashMap
people.clear();
// Printing the HashMap after clearing
System.out.println("HashMap after clear: " + people);
}
}
Output:
HashMap before clear: {Ravi=25, Priya=30, Vijay=35}
HashMap after clear: {}
Real-World Use Case: Resetting a User Session Store
In a real-world scenario, you might use the clear method to reset a user session store, removing all active sessions.
Example
import java.util.HashMap;
public class SessionStore {
public static void main(String[] args) {
// Creating a HashMap with String keys (session IDs) and String values (user names)
HashMap<String, String> sessions = new HashMap<>();
// Adding active sessions
sessions.put("SID001", "Ravi Kumar");
sessions.put("SID002", "Priya Sharma");
sessions.put("SID003", "Vijay Singh");
// Printing the active sessions before clearing
System.out.println("Active sessions before clear: " + sessions);
// Clearing all active sessions
sessions.clear();
// Printing the active sessions after clearing
System.out.println("Active sessions after clear: " + sessions);
}
}
Output:
Active sessions before clear: {SID001=Ravi Kumar, SID002=Priya Sharma, SID003=Vijay Singh}
Active sessions after clear: {}
Conclusion
The HashMap.clear() method in Java provides a way to remove all key-value mappings from a HashMap, effectively resetting it to an empty state. By understanding how to use this method, you can efficiently manage the lifecycle of your HashMap instances in your Java applications. This method is useful when you need to clear the contents of a HashMap without creating a new instance, such as resetting user sessions, clearing caches, or reinitializing data structures.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment