Friday, 21 October 2016

What is TreeSet In Java ?

Previously we have discussed about
Here we learn what is TreeSet In Java.But before that we must know, What is Collection Interface in Java ?
Class TreeSet<E>

Type Parameters:
E - the type of elements maintained by this set

All Implemented Interfaces:

Declaration of TreeSet:
public class TreeSet<E>
extends AbstractSet<E>

TreeSet is a NavigableSet implementation based on TreeMap. TreeSet contains no duplicate elements.TreeSet ordered the elements in natural ordering by default or by a comparator provided at creation time.
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

TreeSet is an example of fail-fast iterator,  means if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.

TreeSet implementation is not synchronized. If multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally, to prevent accidental unsynchronized access to the set:
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
This class is a member of the Java Collections Framework.

Constructor Summary of TreeSet:

Constructor
Description
TreeSet()
Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
TreeSet(Collection<? extends E> c)
Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
TreeSet(Comparator<? super E> comparator)
Constructs a new, empty tree set, sorted according to the specified comparator.
TreeSet(SortedSet<E> s)
Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

Method Summary of TreeSet:

Modifier and Method Name
Description
boolean add(E e)
Adds the specified element to this set if it is not already present.
boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this set.
E ceiling(E e)
Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
void clear()
Removes all of the elements from this set.
Object clone()
Returns a shallow copy of this TreeSet instance.
Comparator<? super E>                comparator()
Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
boolean contains(Object o)
Returns true if this set contains the specified element.
Iterator<E> descendingIterator()
Returns an iterator over the elements in this set in descending order.
NavigableSet<E> descendingSet()
Returns a reverse order view of the elements contained in this set.
E first()
Returns the first (lowest) element currently in this set.
E floor(E e)
Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
SortedSet<E> headSet(E toElement)
Returns a view of the portion of this set whose elements are strictly less than toElement.
NavigableSet<E> headSet(E toElement, boolean inclusive)
Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.
E higher(E e)
Returns the least element in this set strictly greater than the given element, or null if there is no such element.
boolean isEmpty()
Returns true if this set contains no elements.
Iterator<E> iterator()
Returns an iterator over the elements in this set in ascending order.
E last()
Returns the last (highest) element currently in this set.
E lower(E e)
Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
E pollFirst()
Retrieves and removes the first (lowest) element, or returns null if this set is empty.
E pollLast()
Retrieves and removes the last (highest) element, or returns null if this set is empty.
boolean remove(Object o)
Removes the specified element from this set if it is present.
int size()
Returns the number of elements in this set (its cardinality).
NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Returns a view of the portion of this set whose elements range from fromElement to toElement.
SortedSet<E> subSet(E fromElement, E toElement)
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
SortedSet<E> tailSet(E fromElement)
Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
NavigableSet<E> tailSet(E fromElement, boolean inclusive)
Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

Example of TreeSet:

import java.util.Comparator;
import java.util.TreeSet;

class Dog implements Comparator<Integer>{

@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
}
public class TreesetExample {

public static void main(String[] args) {
TreeSet<Integer> t1=new TreeSet<Integer>();
t1.add(3);
t1.add(2);
t1.add(8);
System.out.println("TreeSet with Default nautural order sorting : "+t1);
TreeSet<Integer> t=new TreeSet<Integer>(new Dog());
t.add(5);
t.add(2);
t.add(7);
System.out.println("\nTreeSet with Comparator : "+t);
}

}
Program Output:

TreeSet with Default nautural order sorting : [2, 3, 8]

TreeSet with Comparator : [2, 5, 7]


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment