Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map.clear() vs new Map : Which one will be better? [duplicate]

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?

like image 735
Pankaj Kumar Avatar asked Jul 20 '11 06:07

Pankaj Kumar


People also ask

Can Map has duplicate values?

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.

What does Map Clear () do?

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.

Does Map allow duplicate keys?

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.

Which Map is faster in Java?

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).


4 Answers

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!

like image 109
Vladimir Ivanov Avatar answered Oct 06 '22 00:10

Vladimir Ivanov


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.

like image 40
Tanveer Avatar answered Oct 06 '22 01:10

Tanveer


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

like image 35
sunriser Avatar answered Oct 06 '22 00:10

sunriser


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.

like image 21
Suraj Chandran Avatar answered Oct 05 '22 23:10

Suraj Chandran