TreeSet size Example
TreeSet class size example. This example shows you how to use size method.
TreeSet class size example.public int size()
Returns the number of elements in this set (its cardinality).
Here is the code
/**
* @ # Size.java
* A class repersenting use to size method
* of TreeSet class in java.util package
* version 23 May 2008
* author Rose India
*/
import java.util.*;
public class Size {
public static void main(String args[]) {
TreeSet ts = new TreeSet();
// Add the elements to the set and display it:
System.out.println("Element is added :" + ts.add(10));
System.out.println("Element is added :" + ts.add(20));
System.out.println("Element is added :" + ts.add(15));
System.out.println("All element of the TreeSet " + ts);
System.out.println("Size of the TreeSet " + ts.size());
}
} |
Output
Element is added :true
Element is added :true
Element is added :true
All element of the TreeSet [10, 15, 20]
Size of the TreeSet 3 |
|