Planforexams.com

TreeSet Interface tutorial in Java Collection

The TreeSet Interface implements the Set Interface in java collection and store the elements in a tree like structure. The TreeSet Interface and does not support duplicate elements like List Interface. The TreeSet Interface creates a empty TreeSet when constructor TreeSet() is invoked. The TreeSet Interface allows operations like Add elements, search elements, add elements at specified position, add elements from specified collection ,remove elements, etc.

The TreeSet interface is faster than LinkedHashSet interface but slower in comparison to HashSet due to its tree like structure for traversing the elements

The HashSet Interface class Hierarchy is given below

java.lang.Object
-> java.util.AbstractCollection<E>
->-> java.util.AbstractSet<E>
->->-> Hattersheim java.util.TreeSet<E>

The implemented interfaces by TreeSet Interface

Banning Serializable, Cloneable, Iterable<E>, Collection<E>, NavigableSet<E>, Set<E>, SortedSet<E>

The TreeSet Interface provides the below given constructors

TreeSet Constructor TreeSet Constructor Description
TreeSet()creates a new empty tree set
TreeSet(Collection <? extends e> c)creates a new tree set containing the elements in the specified collection , sorted as elements ordering
TreeSet(Comparator <? super E> comparator)creates a new tree set containing the elements , sorted as specified comparator
TreeSet(SortedSet <E> s) creates a new tree set containing the same elements with same ordering as the specified sorted set

Methods for TreeSet Interface

The TreeSet Interface provides the below given methods for implementation

TreeSet MethodsTreeSet Method Description
add(E e)add the specified element to the set
clear()removes all elements from the set
clone()returns a shallow copy of the TreeSet instance
addAll(Collection < ? extends E > c) allows to insert all elements in a specified collection to the set
ceiling(E e) returns the least element in the set which is greater or equal to the given element
comparator() returns NULL if set uses natural ordering of elements , else returns the comparator used to order elements in the set
contains(object o) returns true if specified element presents in the set
descendingIterator() returns an iterator over the elements in the set in descending order
descendingSet() returns reverse element order from the list
first() returns 1st elements in the set
floor(E e) returns the highest element in the set which is less than or equal to the given element, else NULL
headSet( E toElement) returns partial set view for elements which are less than toElement
higher( E e) returns the lowest element in the set which is less than or equal to the given element, else NULL
isEmpty() returns true if no element present in the set
iterator() returns iterator over the elements in the set
last() returns last element present in the set
lower( E e)returns the highest element in the set which is less than or equal to the given element, else NULL
pollFirst() returns and removes the first lowest element, else null
pollLast() returns and removes the first highest element, else null
remove(object o) remove the specified element if present in the set
size returns Tree Set Size
spliterator()Creates a late-binding and fail-fast Spliterator over the elements in the set
tailSet(E fromElement) returns partial set view whose elements are greater than or equal to fromElement
tailSet(E fromElement boolean inclusive)returns partial set view whose elements are greater than (or equal to, if inclusive is true) fromElement
subSet(E fromElement , E toElement)returns partial set view whose elements range from fromElement, inclusive, to toElement, exclusive

TreeSet Example in Java Collection

import java.util.*;  
class FruitsTreeSet{  
 public static void main(String args[]){  
  //Create TreeSet object as fruits  
  TreeSet<String> fruits =new TreeSet<String>(); 
  // Add elements to fruits   
	   fruits.add("Apple");    
	   fruits.add("Orange");    
	   fruits.add("Banana");   
	   fruits.add("Gauva");  
	   fruits.add("Pears");  
	   Iterator<String> itr =fruits.iterator();  
	   while(itr.hasNext())  
	   {  
				System.out.println(itr.next());  
	   }  
 }  
}  
Exit mobile version