Javatpoint Logo
Javatpoint Logo

Treeset Java Operations

TreeSet is a class in Java that implements the Set interface and is based on a tree data structure. It provides several operations to manage and manipulate a collection of elements in a sorted order. In this article, we will discuss the various TreeSet Java operations that are available.

Creating a TreeSet:

To create a TreeSet, we can use the default constructor or the constructor that takes a Comparator as an argument. The default constructor creates a TreeSet that orders the elements according to their natural ordering, while the constructor that takes a Comparator allows us to define our custom ordering logic.

Example:

Adding elements to a TreeSet:

To add elements to a TreeSet, we can use the add() method. This method adds the element to the TreeSet in its sorted order. If the element already exists in the TreeSet, it will not be added again.

Example:

Removing elements from a TreeSet:

To remove an element from a TreeSet, we can use the remove() method. This method removes the element from the TreeSet if it exists. If the element does not exist, it does nothing.

Example:

Retrieving the first and last elements from a TreeSet:

To retrieve the first and last elements from a TreeSet, we can use the first() and last() methods. These methods return the first and last elements of the TreeSet, respectively.

Example:

Retrieving a subset of elements from a TreeSet:

To retrieve a subset of elements from a TreeSet, we can use the subSet() method. This method returns a view of the TreeSet that contains the elements between the specified range. The range can be specified using two parameters - fromElement and toElement. The elements in the subset are inclusive of fromElement and exclusive of toElement.

Example:

TreeSet<Integer> subSet = treeSet.subSet(5, 20);

Iterating over elements in a TreeSet:

To iterate over the elements in a TreeSet, we can use an iterator or a for-each loop. The iterator() method returns an iterator that can be used to iterate over the elements in the TreeSet.

Example:

Checking the size and emptiness of a TreeSet:

To check the size of a TreeSet, we can use the size() method. This method returns the number of elements in the TreeSet. To check if a TreeSet is empty, we can use the isEmpty() method. This method returns true if the TreeSet is empty, false otherwise.

Example:

here's an example code that uses all the operations we discussed earlier:

TreeSetExample.java

Output:

TreeSet: [25, 20, 15, 10, 5]
TreeSet after removing 15: [25, 20, 10, 5]
First Element: 25
Last Element: 5
Subset: [20, 10, 5]
Iterating over the elements:
25
20
10
5
Size: 4
Is Empty: false

In this example, we have created a TreeSet with custom ordering, added elements to it, removed an element, retrieved the first and last elements, retrieved a subset of elements, iterated over the elements, and checked the size and emptiness of the TreeSet. We also printed the results to the console to verify that the operations were performed correctly.

Conclusion:

In conclusion, TreeSet provides several operations to manage and manipulate a collection of elements in a sorted order. It allows us to add and remove elements, retrieve the first and last elements, retrieve a subset of elements, iterate over the elements, and check the size and emptiness of the TreeSet. With these operations, TreeSet is a useful class for handling sorted collections in Java.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA