Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

TreeMap.clear() method in java

Non Stop Learning...

Overview

The TreeMap.clear() method clear’s all the entries stored in TreeMap.

Syntax:

Tree_Map.clear()

It does not take any parameters and also returns nothing.

Example

Let’s understand with an example:

import java.util.*;
class clearMethod
{
  public static void main(String[] args) 
  {
    TreeMap<Integer, String> tm = new TreeMap<Integer, String>();

    tm.put(3, "Apple");
    tm.put(0, "Mango");
    tm.put(5, "Orange");
    tm.put(10, "Papaya");
    tm.put(7, "Banana");

    System.out.println("The entries  of TreeMap : " + tm);
    tm.clear();
    System.out.println("The TreeMap entries after using clear() method : " + tm);
  }
}

Explanation

  • Line 1: We import the java.util package.

  • Line 2: We create the class clearMethod.

  • Line 6: We create the empty TreeMap to store entries of keys and values.

  • Line 8–12: We insert all the entries in the TreeMap, using the TreeMap.put() method.

  • Line 14: Now, we are going to print the entries of TreeMap inserted.

  • Line 15: We clear the entries in TreeMap using TreeMap.Clear() method.

  • Line 16: Finally, we print and see how the TreeMap entries look after using TreeMap.clear() method.

RELATED TAGS

java
RELATED COURSES

View all Courses

Keep Exploring