在開發中在使用Map時,如果需要將Map作爲臨時的數據存儲和處理,可以不用每次都去新建一個Map,可以使用clear方法來進行清空Map。
例如:
Map<String, Object> map = new HashMap<>();
map.put("test", "demo");
System.out.print(map);
// Clear map
map.clear();
System.out.print(map)
兩次的輸出結果是:
{"test": "demo"}
{}