TreeSet in Java Last Updated : 25 Aug, 2021 Comments Improve Suggest changes Like Article Like Report TreeSet provides an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format. Following are the features of TreeSet. TreeSet uses tree data structure for storage.Objects are stored in sorted, ascending order. But we can iterate in descending order using method TreeSet.descendingIterator().Access and retrieval times are very fast which make TreeSet an excellent choice for storage of large volume of data in sorted format.TreeSet doesn’t use hashCode() and equals() methods to compare it’s elements. It uses compare() (or compareTo()) method to determine the equality of two elements. Important Methods of treeset class: boolean add(E e): This method adds the specified element to this set if it is not already present.E ceiling(E e): This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.boolean contains(Object o): This method returns true if this set contains the specified element.E floor(E e): This method returns the greatest element in this set less than or equal to the given element, or null if there is no such element.E pollFirst(): This method retrieves and removes the first (lowest) element, or returns null if this set is empty.E pollLast(): This method retrieves and removes the last (highest) element, or returns null if this set is empty.boolean remove(Object o): This method removes the specified element from this set if it is present. The following is a very simple TreeSet implementation including TreeSet is sorting, iteration in a TreeSet, retrieving first and last element, and remove an element. Java // Java program to demonstrate working TreeSet collection import java.util.Iterator; import java.util.TreeSet; public class TreeSetExample { public static void main(String[] args) { TreeSet<Integer> ts = new TreeSet<Integer>(); ts.add(10); ts.add(61); ts.add(87); ts.add(39); Iterator<Integer> iterator = ts.iterator(); System.out.print("Tree set data: "); // note that 87 being largest element, appears in // the last. while (iterator.hasNext()) System.out.print(iterator.next() + " "); System.out.println(); // to check if treeset is empty or not. if (ts.isEmpty()) System.out.print("Tree Set is empty."); else System.out.println("Tree Set size: " + ts.size()); // To get the smallest element from the set System.out.println("First data: " + ts.first()); // To get the largest value from set System.out.println("Last data: " + ts.last()); // remove 61 from set. if (ts.remove(61)) System.out.println("Data is removed from tree set"); else System.out.println("Data doesn't exist!"); System.out.print("Now the tree set contain: "); iterator = ts.iterator(); // Displaying the Tree set data while (iterator.hasNext()) System.out.print(iterator.next() + " "); System.out.println(); System.out.println("Now the size of tree set: " + ts.size()); // Remove all ts.clear(); if (ts.isEmpty()) System.out.print("Tree Set is empty."); else System.out.println("Tree Set size: " + ts.size()); } } Output: Tree set data: 10 39 61 87 Tree Set size: 4 First data: 10 Last data: 87 Data is removed from tree set Now the tree set contain: 10 39 87 Now the size of tree set: 3 Tree Set is empty. Please refer TreeSet in Java with Examples for more details. Comment More infoAdvertise with us Next Article TreeSet in Java R Rishabh Mahrsee Improve Article Tags : Java Java-Collections java-treeset Practice Tags : JavaJava-Collections Similar Reads B-Tree in Java A B-tree is a self-balanced tree data structure that will maintain the sorted data and allow for operations such as insertion, deletion and search operations. B-tree is particularly well-suited for systems that need to perform disk-based operations and it minimizes the number of disk accesses requir 6 min read TreeMap in Java TreeMap is a part of the Java Collection Framework. It implements the Map and NavigableMap interface and extends the AbstractMap class. It stores key-value pairs in a sorted order based on the natural ordering of keys or a custom Comparator. It uses a Red-Black Tree for efficient operations (add, re 11 min read TreeSet add() Method in Java The Java.util.TreeSet.add() method in Java TreeSet is used to add a specific element into a TreeSet. The function adds the element only if the specified element is not already present in the set else the function return False if the element is not present in the TreeSet. Syntax: Tree_Set.add(Object 1 min read Java JTree The JTree is a type of GUI(Graphic User Interface) that displays information in a hierarchical way. This intricate component part provides a quite elegant substance of representing relationships among elements in a tree-like structure. In this exploration, we'll delve into the essence of the JTree c 2 min read TreeSet size() Method in Java The Java.util.TreeSet.size() method is used to get the size of the Tree set or the number of elements present in the Tree set. Syntax: Tree_Set.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Set. Below pro 1 min read TreeSet last() Method in Java The Java.util.TreeSet.last() method is used to return the last of the element of a TreeSet. The last element here is being referred to the highest of the elements in the set. If the elements are of integer types, then the largest integer is returned. If the elements are of the string types, then the 3 min read TreeSet first() Method in Java The Java.util.TreeSet.first() method is used to return the first of the element of a TreeSet. The first element here is being referred to the lowest of the elements in the set. If the elements are of integer types then the smallest integer is returned. If the elements are of the string types then th 3 min read TreeSet lower() method in Java The lower(E ele) method of TreeSet class in Java is used to return the greatest element in this set which is strictly less than the given element. If no such element exists in this TreeSet collection then this method returns a NULL. Here, E is the type of the elements maintained by this collection. 2 min read Segment Tree in Java A Segment Tree is a binary tree used for storing intervals or segments. It is allowed to query sum, minimum, maximum or other associative operations over the range of elements in the array efficiently. Each node in the segment tree represents the interval of an array. Leaves of the segment tree corr 5 min read TreeSet addAll() Method in Java The java.util.TreeSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any typ 2 min read Like