I want to process some new data in a HashSet, without any old data needed or the old HashSet object. The old HashSet object isn't referred to elsewhere.
Is it better to simply do hashset = new HashSet<String>()
and let JVM to free the memory of the old HashSet object or should I call hashSet.clear()
and reuse the same HashSet?
According to openJDK, hashSet.clear() is:
public void clear() {
map.clear();
}
and map.clear() :
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
Since map.clear() iterates all the entries, will it be time consuming when the hashSet is large? Which one is recommended in this case, the constructor method or the clear() method?