BACK
This tutorial explains how to clear all elements from treemap using Clear() method.
we can’t clear the particular data, it will clear all the data.
Example :
import java.util.TreeMap;
public class TreeMapClear
{
public static void main(String a[])
{
TreeMap<String, String> hm = new TreeMap<String, String>();
hm.put(“first”, “FIRST INSERTED”);
hm.put(“second”, “SECOND INSERTED”);
hm.put(“third”,”THIRD INSERTED”);
System.out.println(hm);
System.out.println(“Value of second: “+hm.get(“second”));
System.out.println(“Is TreeMap empty? “+hm.isEmpty());
hm.clear();
System.out.println(hm);
}
}
Output :
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
Value of second: SECOND INSERTED
Is TreeMap empty? false
{}