The code demonstrates how to get the size of a TreeSet in Java. In the GetSize class, a TreeSet object called col is created to store strings. Strings such as "Blue", "Red", "Green", "White", "Orange", "Yellow", and "Pink" are added to the col set using the add method.
The System.out.println statement is used to print the elements in the col set, which will be displayed in a sorted order as TreeSet automatically sorts its elements based on their natural order or using a custom comparator if provided.
The col.size() method is used to get the size of the col set, which represents the number of elements in the set. The result is printed using another System.out.println statement.
Note: TreeSet is a collection class in Java that implements the Set interface and stores unique elements in sorted order. It does not allow duplicate elements, and provides efficient operations for adding, removing, and retrieving elements in sorted order. The size() method is used to get the number of elements in the set.
import java.io.*; import java.util.*; public class GetSize { public static void main(String args[]) { TreeSet <String> col = new TreeSet <String>(); col.add("Blue"); col.add("Red"); col.add("Green"); col.add("White"); col.add("Orange"); col.add("Yellow"); col.add("Pink"); System.out.println("TreeSet Elements : " + col); System.out.println("Size of TreeSet : " + col.size()); } }
TreeSet Elements : [Blue, Green, Orange, Pink, Red, White, Yellow] Size of TreeSet : 7
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions