This example shows how to clear LinkedHashMap in Java. This example also shows how to clear or how to remove all mappings from the LinkedHashMap using the clear method.
How to clear LinkedHashMap in Java?
The LinkedHashMap class maintains key-value pairs. There are a couple of ways using which you can clear the LinkedHashMap object.
1. Using the clear method
The clear
method of the LinkedHashMap class removes all the mappings from the map object.
1 |
public void clear() |
The LinkedHashMap object will be empty after this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedHashMap; public class LinkedHashMapClearExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); //will print false System.out.println("LinkedHashMap empty? " + lhmap.isEmpty()); /* * To remove all mappings or to clear LinkedHashMap * object, use the clear method */ lhmap.clear(); //will print true, as the LinkedHashMap is empty now System.out.println("LinkedHashMap empty? " + lhmap.isEmpty()); } } |
Output
1 2 |
LinkedHashMap empty? false LinkedHashMap empty? true |
2. By assigning a new object
You can also assign a new empty LinkedHashMap object to the same reference to make it empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedHashMap; public class LinkedHashMapClearExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); //will print false System.out.println("LinkedHashMap empty? " + lhmap.isEmpty()); /* * Assign a new empty object to * the same LinkedHashMap reference */ lhmap = new LinkedHashMap<Integer, String>(); //will print true, as the LinkedHashMap is empty now System.out.println("LinkedHashMap empty? " + lhmap.isEmpty()); } } |
Output
1 2 |
LinkedHashMap empty? false LinkedHashMap empty? true |
Which method should I use?
Both of these approaches work in a different manner. The clear
method iterates the internal hash table and assigns a null to all the keys, thus making them eligible for garbage collection. The clear
method does not change the capacity of the LinkedHashMap object, it remains the same.
When you assign a new object to the existing LinkedHashMap reference, the entire object including the key and value objects becomes eligible for the garbage collection (if they are not referenced anywhere else). In this case, the old object cannot be reused and it involves a costly operation of creating a new object.
If you are going to add a similar number of mappings to the LinkedHashMap object after clearing it, the clear
method should perform better as it does change the capacity of the map plus it does not involve the creation of a new object. If not, and if the map contains a very large number of mappings, assigning a new object to the same reference will be ok.
This example is a part of the Java LinkedHashMap tutorial with examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap