Java - Delete all elements from a TreeMap
The method
clear() is used to remove any element in the map structure in Java. After calling this method, the list
TreeMap becomes empty.
package codeurjava. TreeMap;
import java.util.TreeMap;
public class TreeMapCopy {
public static void main(String a[]){
TreeMap hmap = new TreeMap();
//add key-value peers
hmap.put(1, "one");
hmap.put(2, "two");
hmap.put(3,"three");
hmap.put(4,"four");
hmap.put(5,"five");
hmap.put(6,"six");
System.out.println("TreeMap before: "+hmap);
System.out.println("Empty TreeMap");
hmap.clear();
System.out.println("TreeMap after: "+hmap);
}
}
Runtime:
TreeMap before: {1=one, 2=two, 3=three, 4=four, 5=five, 6=six}
Empty TreeMap
TreeMap after: {}