Wednesday 8 August 2012

Remove all values from Java TreeMap example


This Java Example shows how to remove all values from TreeMap object or empty TreeMap or clear TreeMap using clear method.

import java.util.TreeMap;

public class EmptyTreeMapExample {

 public static void main(String[] args) {

  // create TreeMap object
  TreeMap tMap = new TreeMap();

  // add key value pairs to TreeMap
  tMap.put("1", "One");
  tMap.put("2", "Two");
  tMap.put("3", "Three");

  /**
   * To remove all values or clear TreeMap use
   * void clear method() of TreeMap class. Clear
   * method removes all key value pairs contained in
   * TreeMap.
   **/

  tMap.clear();

  System.out.println("Total key value pairs in TreeMap are : "
    + tMap.size());
 }
}


The output is:


Total key value pairs in TreeMap are : 0

No comments:

Post a Comment