![]() |
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 Strings Step-by-step implementation:
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
Output
TreeSet: [C, Hibernate, Java, Python, Spring] Elements in TreeSet after adding a new element: [C, C++, Hibernate, Java, Python, Spring] Explanation of the Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |