Creating and Populating a TreeSet in Java Last Updated : 09 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, TreeSet is a pre-defined class that can be used to implement the Set interface and it is a part of the Java collection framework TreeSet is a NavigableSet implementation based on the TreeMap. TreeSet follows the natural order which means elements can be stored in sorted order and it cannot allow the duplicate elements into the TreeSet. In this article, we will learn how to create a TreeSet in Java and add elements to it. Syntax for creating a TreeSet:// Creating a TreeSet of StringsTreeSet<String> treeSet = new TreeSet<>(); Step-by-step implementation:Create the class named the GfGTreeSet and implement the logic into the main method.Create the TreeSet of string names as the treeset and import the related packages into the program.Using TreeSet object to add the elements into the TreeSet with the help of the add() method.Print the result TreeSet of StringsThen add new elements into the TreeSet.Print the result TreeSet after trying to add the new elements into the TreeSet.Program to Create a TreeSet and Add Elements to it in JavaBelow is the program to create and then add elements to a TreeSet: Java // Java program to create a TreeSet and add elements to it import java.util.TreeSet; public class GfGTreeSet { //main method public static void main(String[] args) { // create a TreeSet of Strings TreeSet<String> treeSet = new TreeSet<>(); // adding elements to the TreeSet treeSet.add("C"); treeSet.add("Java"); treeSet.add("Python"); treeSet.add("Spring"); treeSet.add("Hibernate"); // display the elements in the TreeSet System.out.println("TreeSet: " + treeSet); // adding a new element treeSet.add("C++"); // display the elements again after adding a new element System.out.println("Elements in TreeSet after adding a new element: " + treeSet); } } OutputTreeSet: [C, Hibernate, Java, Python, Spring] Elements in TreeSet after adding a new element: [C, C++, Hibernate, Java, Python, Spring] Explanation of the Program:The above program is the example of the creation of the TreeSet and adding elements into the treeset and it is easy to implement the program. First Create the TreeSet, after that adding elements into the treeSet using add() method. Comment More infoAdvertise with us Next Article Creating TreeSet with Comparator by User Define Objects in Java K kadambalamatclo Follow Improve Article Tags : Java Java Programs java-treeset Java Examples Practice Tags : Java Similar Reads 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 Create a TreeMap in Java and Add Key-Value Pairs in it? In Java, a TreeMap maintains elements in a sorted order as it is an implementation of the SortedMap Interface. It stores key-value pairs in a sorted order. In this article, we will explore the creation of TreeMap in Java and learn the step-by-step process of adding key-value pairs. Program to Create 2 min read Get Two Different Objects Into a TreeSet in Java In Java, TreeSet is a part of the Java Collection Framework and is located in the java.util package. It implements the NavigableSet interface and extends the AbstractSet and TreeSet is known for maintaining its elements in sorted order, either based on their natural order. This must be consistent wi 3 min read Java Program to Implement TreeSet API TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether an explicit comparator is provided. This must be consistent with equals if it is to correctly 10 min read Creating TreeSet with Comparator by User Define Objects in Java TreeSet is the implementation class of Set Interface. It follows a natural sorting order or you can customize it using a comparator and it also does not allow duplicates. Syntax: TreeSet<String> gfg= new TreeSet<>(); Below is the normal implementation of the TreeSet: Java // Java program 6 min read Creating Subsets and Headsets from TreeMap with Java A Java TreeMap is a sorted collection that is a component of the Java Collections Framework. Subsets are parts of the map, and headsets are made up of items that are less than or equal to a specified key. In this article, we will learn how to create subsets and headsets from a TreeMap based on speci 3 min read Like