In the following example, we have used the TreeSet collection, which is similar to TreeMap that stores its elements in a tree and maintain order of its elements based on their values. To get the size of TreeSet collection size() method is used. Our TreeSet collection contains 4 elements and the size of the TreeSet can be determine by calling size() method.
Similarly, we have used first()
and last() to retrieve first and
last element present in the TreeSet. Program also shows the method to remove the
element and then display the remaining elements.
Here is the code of program:
import java.util.*;
|
Output of this program:
| C:\vinod\collection>javac TreeSetExample.java C:\vinod\collection>java TreeSetExample Tree Set Example! Tree set data: 12 23 34 45 Tree Set size: 4 First data: 12 Last data: 45 Data doesn't exist! Now the tree set contain: 12 23 34 45 Now the size of tree set: 4 Tree Set is empty. C:\vinod\collection> |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Tree Set Example View All Comments
Post your Comment