Showing posts with label how to delete all element of treemap. Show all posts
Showing posts with label how to delete all element of treemap. Show all posts

Friday, 19 June 2015

How to delete all elements from TreeMap

import java.util.Map;
import java.util.TreeMap;

public class TreeMapDeleteAll {

   
    public static void main(String[] args) {
       
        Map<Integer, String> tm = new TreeMap<Integer,String>();
       
        // add key and values
        tm.put(1,"java");
        tm.put(2,"cpp");
        tm.put(3,"php");
        tm.put(4,"c");
        tm.put(5, "mysql");
       
        //print treemap
   
        System.out.println("Before Clear TreeMap Is="+tm);
       
        //Removes all of the mappings from this map
        tm.clear();
       
        System.out.println("After Clear TreeMap Is ="+tm);
       
    }
   
}




Output:


Before Clear TreeMap Is={1=java, 2=cpp, 3=php, 4=c, 5=mysql}

After Clear TreeMap Is ={}