Friday 6 June 2014

TreeMap : clear() : Removes all the mappings from this map

public void clear()
Removes all of the mappings from this map.

import java.util.*;

class TreeMapClear{
  public static void main(String args[]){
  TreeMap<Integer, String> myMap1 = new TreeMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, "First");
  myMap1.put(2, "Second");
  myMap1.put(3, "Third");
  myMap1.put(4, "Fourth");
  
  System.out.println("Data in myMap1 is");
  System.out.println(myMap1);
  
  System.out.println("\nCalling clear method of myMap1");
  myMap1.clear();
  
  System.out.println("\nData in myMap1 is");
  System.out.println(myMap1);
  }
}

Output
Data in myMap1 is
{1=First, 2=Second, 3=Third, 4=Fourth}

Calling clear method of myMap1

Data in myMap1 is
{}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment