เมธอด clear() ใช้ล้างการแมปปิงระหว่าง key-value pairs ทั้งหมด ทำให้ ConcurrentHashMap ว่างเปล่า
Syntax
public void clear()
ตัวอย่าง
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Map my_map = new ConcurrentHashMap();
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);
}
}
ผลลัพธ์
The map contains the below elements {This=35, is=78, sample=99}
The elements after the clear function is called on it {}
คลาส Demo มีฟังก์ชัน main สร้างอินสแตนซ์ Map ใหม่เพิ่มข้อมูลด้วย put() แสดงข้อมูล เรียก clear() ล้างข้อมูลทั้งหมด แสดงผลอีกครั้งจะเห็นว่า map ว่างเปล่า