Java ConcurrentHashMap - clear()
clear 函式用於清理鍵值對之間的對映。透過這種方式,將清理 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 的類包含主函式。此處,建立一個對映的新例項,並使用“put”函式向其新增元素。顯示元素,然後清除對映。現在,對映將不包含任何內容,當再次顯示對映時可以看到這一點。
廣告