我有一个Map变量,其语法为Map<String, String> testMap = new HashMap<String, String>();
。
这个map里可能会有1000个数据。
当我的应用程序需要新的数据列表时,我必须清空Map。但是当我看到Map.clear()的代码时:
/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
*/
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
我意识到清除方法会循环n次(其中n是Map中数据的数量)。因此,我认为可以通过重新定义Map为testMap = new HashMap<String,String>();
并且以前使用的Map将被垃圾回收。
但我不确定这是否是一个好方法。我正在开发移动应用程序。
你能给我指点吗?