Java TreeSet Class

  • TreeSet is class under Set interface of collection framework.
  • It implements NavigableSet interface which extends SortedSet interface.
  • Elements are ordered by a Comparator which is provided at set creation time or by natural ordering.
  • It maintains the ascending sorting order.
  • TreeSet is not synchronized so if multiple threads accessing it concurrently and any one modifies set entry then it must be synchronized externally.
  • Element's access and retrieval time is very fast from TreeSet so it will much useful when you wants to store large information in ascending order and retrieve any element quickly.
Java TreeSet Class hierarchy
Important methods of TreeSet class are as below.
  • boolean add(E e) : It will insert specified element in TreeSet if it is not already exist.
  • boolean addAll(Collection<? extends E> c) : It will insert all entries of specified collection in to TressSet.
  • E ceiling(E e) : Get least element from the set which is greater than or equals to the given element E. If not found any then it will return null.
  • void clear() : It will remove all elements from set.
  • Object clone() : It will provide shallow copy of this Set instance
  • Comparator<? super E> comparator() : It will return the comparator if any used to order the elements in this set. Return null if set is sorted using natural ordering.
  • boolean contains(Object o) : It will return true if specified element is available in list.
  • Iterator<E> descendingIterator() : It will return the descending order iterator over the set.
  • NavigableSet<E> descendingSet() : It will return reverse order view of set elements.
  • E first() : It will return current first element from set.
  • E floor(E e) : It will provide you greatest element from the set which is less than or equals to the specified element. If not found any then it will return null.
  • SortedSet<E> headSet(E toElement) : It will return portion of set elements which are strictly less than given toElement.
  • NavigableSet<E> headSet(E toElement, boolean inclusive) : It will return portion of set elements which are less than given toElement. toElement will be included if inclusive is true.
  • E higher(E e) : It will return least element from set which is strictly greater than the specified element. Return null fi not found any.
  • boolean isEmpty() : It will return true if TreeSet is empty.
  • Iterator<E> iterator() : It will return the iterator over elements of set.
  • E last() : It will return last element from set.
  • E lower(E e) : It will return greatest element from set which is strictly less than the given element. If not found any then it will return null.
  • E pollFirst() : It will access and remove first (lowest) element from the set. If set is empty then it will return null.
  • E pollLast() : It will access and remove last (highest) element from the set. If set is empty then it will return null.
  • boolean remove(Object o) : It will remove given element from the set(if it is preset).
  • int size() :It will return size of set.
  • NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) : It will return portion of set starting from fromElement to toElement. fromElement will be included in set portion if fromInclusive is true. toElement will be included in set portion if toInclusive is true.
  • SortedSet<E> subSet(E fromElement, E toElement) : It will return portion of set starting from fromElement(Included) to toElement(Excluded).
  • SortedSet<E> tailSet(E fromElement) : It will return tail set starting from fromElement. fromElement will be included in set.
  • NavigableSet<E> tailSet(E fromElement, boolean inclusive) : It will return tail set starting from fromElement. fromElement will be included in set if inclusive is true.
Below given example will show you usage of few TreeSet class's method's demo.

TreeSet Example :
package JAVAExamples;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

 public static void main(String[] args) {
  // Create TreeSet.
  TreeSet<Integer> ts = new TreeSet<Integer>();
  // Add elements in TreeSet.
  ts.add(5);
  ts.add(79);
  ts.add(9);
  ts.add(52);
  ts.add(3);
  ts.add(69);
  ts.add(52);
  ts.add(76);
  ts.add(15);
  ts.add(42);
  ts.add(93);
  ts.add(334);

  // Print TreeSet.
  System.out.println("TreeSet elements are : " + ts);
  // Get ceiling value from treeset.
  System.out.println("ceiling of 45 in treeset is : " + ts.ceiling(45));
  // Get clone of treeset.
  System.out.println("clone of treeset is : " + ts.clone());
  // Check if treeset contains given element.
  System.out.println("treeset contains 76? : " + ts.contains(76));

  // Print treeset in descending order.
  Iterator iterator;
  iterator = ts.descendingIterator();
  System.out.println("Tree set data in descending order: ");
  while (iterator.hasNext()) {
   System.out.println(iterator.next() + " ");
  }

  // Get first element from treeset.
  System.out.println("First element in treeset is : " + ts.first());
  // Get last element from treeset.
  System.out.println("Last element in treeset is : " + ts.last());
  // Get floor element from treeset.
  System.out.println("floor element of 51 is : " + ts.floor(51));
  // Get size of treeset.
  System.out.println("Size of treeset is : " + ts.size());
  // Remove first element from treeset.
  ts.pollFirst();
  System.out.println("TreeSet elements after pollFirst : " + ts);
  // Remove last element from treeset.
  ts.pollLast();
  System.out.println("TreeSet elements after pollLast : " + ts);
  // Get tailset exclusive fromElement.
  System.out.println("tail set from 69 is : " + ts.tailSet(69, false));
  // Get subset exclusive fromElement and toElement.
  System.out.println("subset set from 9 and 76 exclusive is : "+ ts.subSet(9, false, 76, false));
  // Get subset inclusive fromElement and toElement.
  System.out.println("subset set from 9 and 76 inclusive is : "+ ts.subSet(9, true, 76, true));
 }
}

Output :
TreeSet elements are : [3, 5, 9, 15, 42, 52, 69, 76, 79, 93, 334]
ceiling of 45 in treeset is : 52
clone of treeset is : [3, 5, 9, 15, 42, 52, 69, 76, 79, 93, 334]
treeset contains 76? : true
Tree set data in descending order: 
334 
93 
79 
76 
69 
52 
42 
15 
9 
5 
3 
First element in treeset is : 3
Last element in treeset is : 334
floor element of 51 is : 42
Size of treeset is : 11
TreeSet elements after pollFirst : [5, 9, 15, 42, 52, 69, 76, 79, 93, 334]
TreeSet elements after pollLast : [5, 9, 15, 42, 52, 69, 76, 79, 93]
tail set from 69 is : [76, 79, 93]
subset set from 9 and 76 exclusive is : [15, 42, 52, 69]
subset set from 9 and 76 inclusive is : [9, 15, 42, 52, 69, 76]

No comments:

Post a Comment