How to Get Size and Elements Nubmer of TreeSet in Java


This example focuses on how to get the size or nubmer of elements stored in Java TreeSet object with size method.

Source Code

package com.beginner.examples;

import java.util.TreeSet;

public class GetSizeOfJavaTreeSet {

	public static void main(String[] args) {
		
		//create TreeSet.
		TreeSet treeSet = new TreeSet();
		
		treeSet.add("A");
		treeSet.add("B");
		treeSet.add("C");
		treeSet.add("D");
		
		/*
		 * To get the size of TreeSet use size() method of TreeSet class.
		 */
		System.out.println("The size of TreeSet is : " + treeSet.size());

	}

}

Output:

The size of TreeSet is : 4

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments