LinkedHashMap clear() in Java

clear(): This method is available in java.util.LinkedHashMap class of Java.

Syntax:

void java.util.LinkedHashMap.clear()

This method removes all of the mappings from this map.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.LinkedHashMap;

public class LinkedHashMapclear {

    public static void main(String args[]) {

        LinkedHashMap<String, Integer> linkedHashMap =
new LinkedHashMap<String, Integer>();

        linkedHashMap.put("Java", 1);
        linkedHashMap.put("Hello", 2);

        System.out.println("Map before clear: " + linkedHashMap);

        linkedHashMap.clear();

        System.out.println("Map after clear: " + linkedHashMap);
    }

}

Output:

Map before clear: {Java=1, Hello=2}

Map after clear: {}


No comments:

Post a Comment