How to Add or Append Elements to End of a TreeSet in Java? Last Updated : 05 Feb, 2024 Comments Improve Suggest changes Like Article Like Report A TreeSet is a sorted set implementation of the Set interface based on a TreeMap. It uses a Red-Black tree to store elements. Elements are sorted according to their natural ordering, or a custom Comparator provided at the time of TreeSet creation. It does not allow duplicate elements or null values.It provides log(n) time complexity for basic operations like add, remove, and contains due to its tree implementation.Common methods include add(), remove(), contains(), first(), last(), lower(), higher() etc.In this article, we will learn about how to add elements to the end of a TreeSet in Java. Syntax:TreeSet.add(object element)Program to add elements to the end of a TreeSet in JavaBelow is the implementation to append elements to the end of a TreeSet. Java // Java Program to add elements to the end of a TreeSet import java.util.TreeSet; public class TreeSetExample { public static void main(String[] args) { // Create a TreeSet TreeSet<String> courseSet = new TreeSet<>(); // Add elements to the TreeSet courseSet.add("Java"); courseSet.add("Python"); courseSet.add("C++"); // Print the TreeSet System.out.println("TreeSet elements: " + courseSet); // Add elements to the end of TreeSet courseSet.add("DSA"); courseSet.add("Full Stack Web Development"); // Print the TreeSet after adding elements System.out.println("TreeSet elements after adding: " + courseSet); } } OutputTreeSet elements: [C++, Java, Python] TreeSet elements after adding: [C++, DSA, Full Stack Web Development, Java, Python] Explanation of the Program:In the above program, a TreeSet of Strings is created to store course names.Some course names are added to the TreeSet which sorts them automatically.The initial TreeSet elements are printed in sorted order.More courses are added and TreeSet keeps them sorted in insertion order.The final TreeSet after all additions is printed, still maintaining sorted order.Note: TreeSet is automatically sorted, so the final output shows the elements in their natural order. Comment More infoAdvertise with us Next Article How to Add or Append Elements to End of a TreeSet in Java? P pranay0911 Follow Improve Article Tags : Java Java Programs Java-Collections java-treeset Java Examples +1 More Practice Tags : JavaJava-Collections Similar Reads Java Program to Find the Index of the TreeSet Element Unlike the List classes like ArrayList or a LinkedList, the TreeSet class does not allow accessing elements using the index. There are no direct methods to access the TreeSet elements using the index and thus finding an index of an element is not straightforward. Methods: There are primarily three s 6 min read How to sort TreeSet in descending order in Java? Given a TreeSet in Java, task is to sort elements of TreeSet in Descending Order (descreasing order).Examples: Input : Set: [2, 3, 5, 7, 10, 20] Output : Set: [20, 10, 7, 5, 3, 2] Input : Set: [computer, for, geeks, hello] Output : Set: [hello, geeks, for, computer] Approach: To make a TreeSet Eleme 1 min read How to Add Custom Class Objects to the TreeSet in Java? TreeSet is an implementation of the SortedSet interface in java that uses a red-black tree for storage. By default, It maintains an ascending order. It contains unique elements only. It doesn't allow null elements. Access and retrieval times are quite fast. To add the user-defined object into TreeSe 3 min read How to Create a TreeSet with a List in Java? TreeSet is an implementation of the SortedSet interface in Java that uses a Tree for storage. TreeSet can be created from List by passing the List to the TreeSet constructor in Java or we can traverse complete List and adding each element of the List to the TreeSet. Example: Input : List = [a, b, c] 3 min read How to Iterate Over the Elements in a TreeSet in Natural Order in Java? In Java, to iterate over the elements of a TreeSet in their Natural Order, one must either use a custom comparator provided or traverse the elements in ascending order based on their natural ordering. A TreeSet in Java keeps up with its components in arranged requests. In this article, we will learn 2 min read Like