Remove specific element from TreeSet in Java

In this tutorial, we will learn how to Remove specific element from TreeSet in Java.

TreeSet in Java extends AbstractSet and implements the NavigableSet interface. It creates a collection that uses a tree for storage. The objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly. TreeSet is similar to HashSet, the difference is that TreeSet sorts the elements in ascending order and doesn’t allow null elements.

If we want to remove a specific element from TreeSet in Java, then this class provides remove() method to remove specific element. We will use this method to remove specific element from TreeSet . The syntax of remove() method is:

Syntax of remove() method:

Parameters:
obj – object to be removed from the Set if present

Returns:
true if the Set contains the specified element

Example:

Output:

Related posts

Leave a Comment