ฟังก์ชันล้างใช้เพื่อล้างการจับคู่ระหว่างคู่ค่าคีย์ ด้วยวิธีนี้ การแมป ConcurrentHashMap จะถูกล้าง
ไวยากรณ์
public void clear()
เรามาดูตัวอย่างกัน −
ตัวอย่าง
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);
}
} ผลลัพธ์
The map contains the below elements {This=35, is=78, sample=99}
The elements after the clear function is called on it {} คลาสชื่อ Demo มีฟังก์ชันหลัก ที่นี่ อินสแตนซ์ใหม่ของแผนที่จะถูกสร้างขึ้น และเพิ่มองค์ประกอบเข้าไปโดยใช้ฟังก์ชัน 'วาง' องค์ประกอบจะปรากฏขึ้นและถัดไปจะล้างแผนที่ ตอนนี้แผนที่จะไม่มีอะไรและสามารถมองเห็นได้เมื่อแสดงแผนที่อีกครั้ง