Java Program to Remove All Elements of TreeSet Collection

You are currently viewing Java Program to Remove All Elements of TreeSet Collection

Java program to remove all elements of TreeSet collection

// Java program to remove all elements of 
// TreeSet collection

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree = new TreeSet < Integer > ();

    tree.add(10);
    tree.add(20);
    tree.add(30);
    tree.add(40);
    tree.add(50);
    tree.add(60);

    System.out.println("TreeSet elements: " + tree);
    tree.clear();
    System.out.println("TreeSet elements: " + tree);
  }
}

Output

TreeSet elements: [10, 20, 30, 40, 50, 60]
TreeSet elements: []

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply