Java TreeSet

Java TreeSet
Syntax:
java.lang.Object
     java.util.AbstractCollection
         java.util.AbstractSet
             java.util.TreeSet

Note: E - the type of elements maintained by this set

  • Java TreeSet class implements the Set interface that uses a tree for storage. 
  • The objects of the TreeSet class are stored in ascending order.
  • The important properties about Java TreeSet class are:
    • It contains unique elements only like HashSet.
    • It can access and retrieval times are quite fast.
    • It doesn't allow null element.
    • It is non synchronized.
    • It is maintains ascending order.
Constructor:
  • TreeSet(): It is used to construct an empty tree set that will be sorted in ascending order according to the natural order of the tree set.
  • TreeSet(Collection c): It is used to build a new tree set that contains the elements of the collection c.
  • TreeSet(Comparator comparator): It is used to construct an empty tree set that will be sorted according to a given comparator.
  • TreeSet(SortedSet s): It is used to build a TreeSet that contains the elements of the given SortedSet.
Methods:
How to declare object of TreeSet?

  • Syntax: TreeSet al=new TreeSet();
  • Example:

TreeSet al=new TreeSet();

How to add elements in TreeSet?
  • boolean add(E e): It is used to add the specified element to this set if it is not already present
  • Example:
al.add(11);
al.add(21);

How to display all elements as initial set?
  • Example:
System.out.println("Intial Set: "+al);

How to display reverset elements set?
  • NavigableSet descendingSet(): It returns the elements in reverse order.
  • Example:
System.out.println("Reverse Set: "+set.descendingSet());

How to display all elements of TreeSet?
  • Example:
Iterator itr=al.iterator();  
           while(itr.hasNext()){
               System.out.println(itr.next());
                }

How to display all elements of TreeSet descending order?
  • Example:
Iterator i=al.descendingIterator();
         while(i.hasNext())
         {
             System.out.println(i.next());
         }

How to retrieve and remove the highest value from Treeset?
  • E pollLast(): It is used to retrieve and remove the highest(last) element.
  • Example:
TreeSet set=new TreeSet();  
         set.add(24);
         set.add(66);
         set.add(12);
         set.add(15);
System.out.println("Lowest Value: "+set.pollLast());

How to retrieve and remove the lowest value from Treeset?
  • E pollFirst(): It is used to retrieve and remove the lowest(first) element.
  • Example:
TreeSet set=new TreeSet();  
         set.add(44);
         set.add(66);
         set.add(12);
         set.add(15);
System.out.println("Highest Value: "+set.pollFirst());

Other methods:
  • boolean addAll(Collection c): It is used to add all of the elements in the specified collection to this set.
  • E ceiling(E e): It returns the equal or closest greatest element of the specified element from the set, or null there is no such element.
  • E higher(E e): It returns the closest greatest element of the specified element from the set, or null there is no such element.
  • E lower(E e): It returns the closest least element of the specified element from the set, or null there is no such element.
  • boolean contains(Object o): It returns true if this set contains the specified element.
  • void clear(): It is used to remove all of the elements from this set.
  • boolean isEmpty(): It returns true if this set contains no elements.
  • boolean remove(Object o): It is used to remove the specified element from this set if it is present
  • E first(): It returns the first (lowest) element currently in this sorted set.
  • E last(): It returns the last (highest) element currently in this sorted set.
  • int size(): It returns the number of elements in this set.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form