Friday, October 10, 2025
HomeLanguagesJavaLinkedHashMap clear() Method in Java

LinkedHashMap clear() Method in Java

The java.util.LinkedHashMap.clear() is an inbuilt method of LinkedHashMap class in Java and is used to clear all the elements or the mappings from the denoted LinkedHashMap. The map will become empty after the operation is performed.

Syntax:

Linked_Hash_Map.clear()

Parameters: The method does not take any parameters.

Return Value: The method does not return any value.

Below programs illustrate the java.util.LinkedHashMap.clear() method:
Program 1: Mapping String Values to Integer Keys.




// Java code to illustrate the clear() method
import java.util.*;
  
public class Linked_Hash_Map_Demo {
    public static void main(String[] args)
    {
        // Creating an empty LinkedHashMap
        LinkedHashMap<Integer, String> li_hash_map =
        new LinkedHashMap<Integer, String>();
  
        // Mapping string values to int keys
        li_hash_map.put(10, "Geeks");
        li_hash_map.put(15, "4");
        li_hash_map.put(20, "Geeks");
        li_hash_map.put(25, "Welcomes");
        li_hash_map.put(30, "You");
  
        // Displaying the LinkedHashMap
        System.out.println("Initial Mappings are: " + li_hash_map);
  
        // Clearing the linked hash map using clear()
        li_hash_map.clear();
  
        // Displaying the final HashMap
        System.out.println("Finally the maps look like this: " + li_hash_map);
    }
}


Output:

Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Finally the maps look like this: {}

Program 2: Mapping Integer Values to String Keys.




// Java code to illustrate the clear() method
import java.util.*;
  
public class Linked_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty LinkedHashMap
        LinkedHashMap<String, Integer> li_hash_map =
        new LinkedHashMap<String, Integer>();
  
        // Mapping int values to string keys
        li_hash_map.put("Geeks", 10);
        li_hash_map.put("4", 15);
        li_hash_map.put("Geeks", 20);
        li_hash_map.put("Welcomes", 25);
        li_hash_map.put("You", 30);
  
        // Displaying the LinkedHashMap
        System.out.println("Initial Mappings are: " + li_hash_map);
  
        // Clearing the linked hash map using clear()
        li_hash_map.clear();
  
        // Displaying the final HashMap
        System.out.println("Finally the maps look like this: " + li_hash_map);
    }
}


Output:

Initial Mappings are: {Geeks=20, 4=15, Welcomes=25, You=30}
Finally the maps look like this: {}

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS