![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java ConcurrentHashMap - clear()
The clear function is used to clear up the mapping between the key value pairs. This way, the ConcurrentHashMap mappings would be cleared.
Syntax
public void clear()
Let us see an example −
Example
import java.util.concurrent.ConcurrentHashMap; import java.util.*; public class Demo{ public static void main(String[] args){ Map<String, String> my_map = new ConcurrentHashMap<String, String>(); my_map.put("This", "35"); my_map.put("is", "78"); my_map.put("sample", "99"); System.out.println("The map contains the below elements " + my_map); my_map.clear(); System.out.println("The elements after the clear function is called on it " + my_map); } }
Output
The map contains the below elements {This=35, is=78, sample=99} The elements after the clear function is called on it {}
A class named Demo contains the main function. Here, a new instance of the Map is created, and elements are added to it using the ‘put’ function. The elemnts are displayed and next, the map is cleared. Now, the map won’t contain anything and this can be seen when the map is displayed again.
Advertisements