Tutorials Java Java TreeSet Example

Java TreeSet Example

In the following example, we will see how to use TreeSet Collection in java. This example includes,

  • How to create an TreeSet
  • How to display element of Treeset
  • Adding data to TreeSet
  • Deleting data from TreeSet

Things to know about TreeSet

  1. TreeSet is similar to TreeMap that stores its elements in a tree and maintain order of its elements based on their values. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.
  2. To get the size of TreeSet collection size() method is used.
  3. The size of the TreeSet can be determine by calling size() method.
  4. remove() method can be used to remove an item/object
  5. clear() method removes all object and makes the TreeSet empty
  6. first() and last() method is used to get the first and last element in TreeSet

TreeSet Example

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

	public static void main(String[] args) {
		// adding Integer to treeset
		TreeSet<Integer> treeSet = new TreeSet<Integer>();

		// Check empty or not
		if (treeSet.isEmpty()) {
			System.out.print("TreeSet is empty.");
		} else {
			System.out.println("TreeSet size: " + treeSet.size());

		// Looping and Displaying the Tree set data using Iterator
		Iterator<Integer> iterator = treeSet.iterator();
		while (iterator.hasNext()) {
			System.out.print(iterator.next() + "\t");

		// Retrieve first data from tree set
		System.out.println("\nFirst Element: " + treeSet.first());

		// Retrieve last data from tree set
		System.out.println("Last Element: " + treeSet.last());

		// remove element by value
		boolean flag = treeSet.remove(40);
		if (flag) {
			System.out.println("40 is removed!");
		} else {
			System.out.println("40 doesn't exist!");

		System.out.println("New size after delete: " + treeSet.size());

		// Looping and Displaying the Tree set data using Iterator
		iterator = treeSet.iterator();
		while (iterator.hasNext()) {
			System.out.print(iterator.next() + "\t");

		// Remove all data
		if (treeSet.isEmpty()) {
			System.out.print("\nTree Set is empty.");
		} else {
			System.out.println("\nTree Set size: " + treeSet.size());


TreeSet size: 5
20	22	30	40	50	
First Element: 20
Last Element: 50
40 is removed!
New size after delete: 4
20	22	30	50	
Tree Set is empty.
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Shivam Singh
Shivam Singh
7 years ago

Nice article and visit java collection programs java proficiency

Previous Post
Android Tutorials

ActionBar with Custom View Example in Android

Next Post

How to Create an Android ListView with Section Header

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
Would love your thoughts, please comment.x