Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – TreeSet

Declaration

class TreeSet<E>

TreeSet class extends abstract class AbstractSet & implements the NavigableSet interface.

Duplicates are not allowed.

Elements insertion order is not preserved.

Elements are stored according to default ascending order.

Only homogeneous (same type) elements are allowed.

List of TreeSet Constructors (CLICK HERE)

List of TreeSet Methods (CLICK HERE)

Example

 import java.util.TreeSet;

 public class TreeSetGenDemo1 
 {
     public static void main(String args[])
     {
         TreeSet<Integer> ts = new TreeSet<Integer>();
         ts.add(10);
         ts.add(20);
         ts.add(30);
         ts.add(40);
         ts.add(50);
         System.out.println("TreeSet :: "+ts);
         System.out.println("subSet(20,50) of TreeSet "+ts.subSet(20, 50));
         System.out.println("tailSet(30) of TreeSet "+ts.tailSet(30));
         System.out.println("First element of TreeSet :: "+ts.first());
         System.out.println("Last element of TreeSet :: "+ts.last());
     }
 }

Output

TreeSet :: [10, 20, 30, 40, 50]
subSet(20,50) of TreeSet [20, 30, 40]
tailSet(30) of TreeSet [30, 40, 50]
First element of TreeSet :: 10
Last element of TreeSet :: 50

 

Leave a reply