I have a Map as syntax as Map<String, String> testMap = new HashMap<String, String>();
.
In this map there can be 1000 data.
When my application requires to new list of data, then I must clear the Map. But when I saw the code of Map.clear() as
/**
* 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;
}
I realize that clear method goes in loop for n times (Where n is number of data in Map). So I thought there can be a way to redefine that Map as testMap = new HashMap<String, String>();
and previously used Map will be Garbage collected.
But I am not sure this will be a good way. I am working on mobile application.
Can you please guide me?
Map doesn't allow duplicate keys, but it allows duplicate values. HashMap and LinkedHashMap allows null keys and null values but TreeMap doesn't allow any null key or value. Map can't be traversed so you need to convert it into Set using keySet() or entrySet() method.
Map. clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map collection. Parameters: The method does not accept any parameters. Return Value: The method does not return any value.
Duplicate keys are not allowed in a Map. Basically, Map Interface has two implementation classes HashMap and TreeMap the main difference is TreeMap maintains an order of the objects but HashMap will not.
The result is that HashMap is five times faster than Dictionary, almost the same as the performance on the Java VM (Consider the performance loss caused by transplantation).
Complicated question. Let's see what happens.
You instantiate a new instance, which is backed with new array. So, garbage collector should clear all the key and values from the previous map, and clear the reference to itself. So O(n) algorithm is executed anyway, but in the garbage collector thread. For 1000 records you won't see any difference.
BUT. The performance guide tells you that it is always better not to create new objects, if you can. So I would go with clear()
method.
Anyway, try both variants and try to measure. Always measure!
When you say Map.clear()
on a Map of size n
... You are asking the GC to clean up 2*n
(Key & Value) objects. When you say null
to the same Map, you are asking the GC to clean up 2*n+1
(1 for the Map itself) objects. Then you will have to create a new Map instance yet another overhead. So go for Map.clear()
. You will be wise to preset the size of the Map while instantiating it.
I thought Creating object in java more expensive in terms of memory,so it is better to you go with .clear()
,so you are using same object instead of creating new one
The idea of having clear() method is to remove references to other objects from the map, so that the key/values are not held up from gcing if the "map is referenced somewhere else".
But if your map is a local map only used by your specific code( i.e. "map is 'not' referenced somewhere else") then go ahead and use a new map instead, but setting a 1000 references to null wont be a big performance hit anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With