Sunday, February 21, 2016

Java Treeset Class

| Sunday, February 21, 2016
  • TreeSet is course of teaching nether Set interface of collection framework.
  • It implements NavigableSet interface which extends SortedSet interface.
  • Elements are ordered by a Comparator which is provided at laid creation fourth dimension or yesteryear natural ordering.
  • It maintains the ascending sorting order.
  • TreeSet is non synchronized thence if multiple threads accessing it concurrently in addition to whatever i modifies laid entry in addition to thence it must live synchronized externally.
  • Element's access in addition to retrieval fourth dimension is really fast from TreeSet thence it volition much useful when you lot wants to shop large information inwards ascending social club in addition to hollo back whatever chemical cistron quickly.
TreeSet is course of teaching nether Set interface of collection framework Java TreeSet Class
Important methods of TreeSet course of teaching are equally below.
  • boolean add(E e) : It volition insert specified chemical cistron inwards TreeSet if it is non already exist.
  • boolean addAll(Collection<? extends E> c) : It volition insert all entries of specified collection inwards to TressSet.
  • E ceiling(E e) : Get to the lowest degree chemical cistron from the laid which is greater than or equals to the given chemical cistron E. If non institute whatever in addition to thence it volition render null.
  • void clear() : It volition take away all elements from set.
  • Object clone() : It volition render shallow re-create of this Set instance
  • Comparator<? super E> comparator() : It volition render the comparator if whatever used to social club the elements inwards this set. Return goose egg if laid is sorted using natural ordering.
  • boolean contains(Object o) : It volition render truthful if specified chemical cistron is available inwards list.
  • Iterator<E> descendingIterator() : It volition render the descending social club iterator over the set.
  • NavigableSet<E> descendingSet() : It volition render contrary social club thought of laid elements.
  • E first() : It volition render electrical flow receive down chemical cistron from set.
  • E floor(E e) : It volition render you lot greatest chemical cistron from the laid which is less than or equals to the specified element. If non institute whatever in addition to thence it volition render null.
  • SortedSet<E> headSet(E toElement) : It volition render subdivision of laid elements which are strictly less than given toElement.
  • NavigableSet<E> headSet(E toElement, boolean inclusive) : It volition render subdivision of laid elements which are less than given toElement. toElement volition live included if inclusive is true.
  • E higher(E e) : It volition render to the lowest degree chemical cistron from laid which is strictly greater than the specified element. Return goose egg fi non institute any.
  • boolean isEmpty() : It volition render truthful if TreeSet is empty.
  • Iterator<E> iterator() : It volition render the iterator over elements of set.
  • E last() : It volition render terminal chemical cistron from set.
  • E lower(E e) : It volition render greatest chemical cistron from laid which is strictly less than the given element. If non institute whatever in addition to thence it volition render null.
  • E pollFirst() : It volition access in addition to take away receive down (lowest) chemical cistron from the set. If laid is empty in addition to thence it volition render null.
  • E pollLast() : It volition access in addition to take away terminal (highest) chemical cistron from the set. If laid is empty in addition to thence it volition render null.
  • boolean remove(Object o) : It volition take away given chemical cistron from the set(if it is preset).
  • int size() :It volition render size of set.
  • NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) : It volition render subdivision of laid starting from fromElement to toElement. fromElement volition live included inwards laid subdivision if fromInclusive is true. toElement volition live included inwards laid subdivision if toInclusive is true.
  • SortedSet<E> subSet(E fromElement, E toElement) : It volition render subdivision of laid starting from fromElement(Included) to toElement(Excluded).
  • SortedSet<E> tailSet(E fromElement) : It volition render tail laid starting from fromElement. fromElement volition live included inwards set.
  • NavigableSet<E> tailSet(E fromElement, boolean inclusive) : It volition render tail laid starting from fromElement. fromElement volition live included inwards laid if inclusive is true.
Below given representative volition demo you lot usage of few TreeSet class's method's demo.

TreeSet Example :
package JAVAExamples;  import java.util.Iterator; import java.util.TreeSet;  populace course of teaching TreeSetExample {   populace static void main(String[] args) {   // Create TreeSet.   TreeSet<Integer> ts = novel TreeSet<Integer>();   // Add elements inwards 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 inwards 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 inwards descending order.   Iterator iterator;   iterator = ts.descendingIterator();   System.out.println("Tree laid information inwards descending order: ");   spell (iterator.hasNext()) {    System.out.println(iterator.next() + " ");   }    // Get receive down chemical cistron from treeset.   System.out.println("First chemical cistron inwards treeset is : " + ts.first());   // Get terminal chemical cistron from treeset.   System.out.println("Last chemical cistron inwards treeset is : " + ts.last());   // Get flooring chemical cistron from treeset.   System.out.println("floor chemical cistron of 51 is : " + ts.floor(51));   // Get size of treeset.   System.out.println("Size of treeset is : " + ts.size());   // Remove receive down chemical cistron from treeset.   ts.pollFirst();   System.out.println("TreeSet elements later pollFirst : " + ts);   // Remove terminal chemical cistron from treeset.   ts.pollLast();   System.out.println("TreeSet elements later pollLast : " + ts);   // Get tailset exclusive fromElement.   System.out.println("tail laid from 69 is : " + ts.tailSet(69, false));   // Get subset exclusive fromElement in addition to toElement.   System.out.println("subset laid from ix in addition to 76 exclusive is : "+ ts.subSet(9, false, 76, false));   // Get subset inclusive fromElement in addition to toElement.   System.out.println("subset laid from ix in addition to 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 inwards treeset is : 52 clone of treeset is : [3, 5, 9, 15, 42, 52, 69, 76, 79, 93, 334] treeset contains 76? : truthful Tree laid information inwards descending order:  334  93  79  76  69  52  42  xv  ix  v  iii  First chemical cistron inwards treeset is : iii Last chemical cistron inwards treeset is : 334 flooring chemical cistron of 51 is : 42 Size of treeset is : eleven TreeSet elements later pollFirst : [5, 9, 15, 42, 52, 69, 76, 79, 93, 334] TreeSet elements later pollLast : [5, 9, 15, 42, 52, 69, 76, 79, 93] tail laid from 69 is : [76, 79, 93] subset laid from ix in addition to 76 exclusive is : [15, 42, 52, 69] subset laid from ix in addition to 76 inclusive is : [9, 15, 42, 52, 69, 76]

Related Posts