java.util.TreeMap.clear() Method

Advertisements


Description

The clear() method is used to remove all of the mappings from this map. The map will be empty after this call returns.

Declaration

Following is the declaration for java.util.TreeMap.clear() method.

public void clear()

Parameters

  • NA

Return Value

NA

Exception

  • NA

Example

The following example shows the usage of java.util.TreeMap.clear() method.

package com.tutorialspoint;

import java.util.*;

public class TreeMapDemo {
   public static void main(String[] args) {
      // creating tree map 
      NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>();
      
      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");
      
      System.out.println("Entries in the Map: "+ treemap);
      
      // clearing the map
      System.out.println("Clearing the Map");              
      treemap.clear();
      
      System.out.println("Entries in the Map: "+ treemap);
   }    
}

Let us compile and run the above program, this will produce the following result.

Entries in the Map: {1=one, 2=two, 3=three, 5=five, 6=six}
Clearing the Map
Entries in the Map: {}



Advertisements
Advertisements