WeakHashMap clear() in Java

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

Syntax:

void java.util.WeakHashMap.clear()

This method removes all of the mappings from this map.The map will be empty after this call returns.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.WeakHashMap;

public class WeakHashMapclear {
    public static void main(String[] args) {

        WeakHashMap<String, String> weakHashMap =
new WeakHashMap<String, String>();

        weakHashMap.put("Java", "Program");
        weakHashMap.put("Hello", "World");

        System.out.println("Before clear " + weakHashMap);
        weakHashMap.clear();
        System.out.println("After clear " + weakHashMap);
    }
}

Output:

Before clear {Hello=World, Java=Program} After clear {}


No comments:

Post a Comment