Remove element from TreeSet

boolean remove(Object o)
Removes the specified element from this set if it is present.

  import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    TreeSet<Integer> tSet = new TreeSet<Integer>();
    System.out.println("Size of TreeSet : " + tSet.size());

    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));

    System.out.println(tSet.size());

    // remove one element from TreeSet using remove method

    tSet.remove(new Integer("1"));
    System.out.println("Size of TreeSet after removal : " + tSet.size());
  }
}
  

The output:


Size of TreeSet : 0
3
Size of TreeSet after removal : 2
Home 
  Java Book 
    Collection  

TreeSet:
  1. TreeSet class
  2. Create TreeSet objects
  3. Add elements to a TreeSet
  4. Get the least and greatest element in this TreeSet'>
  5. The first and last element in this TreeSet
  6. Get the head set and tail set
  7. Remove element from TreeSet
  8. Get the size of this TreeSet
  9. Get a subset from this TreeSet