- TreeSet is shape 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 past times natural ordering.
- It maintains the ascending sorting order.
- TreeSet is non synchronized hence if multiple threads accessing it concurrently together with whatever 1 modifies laid entry hence it must live synchronized externally.
- Element's access together with retrieval fourth dimension is real fast from TreeSet hence it volition much useful when you lot wants to shop large information inwards ascending fellowship together with scream upwards whatever chemical cistron quickly.
- 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 constitute whatever hence it volition render null.
- void clear() : It volition withdraw all elements from set.
- Object clone() : It volition furnish shallow re-create of this Set instance
- Comparator<? super E> comparator() : It volition render the comparator if whatever used to fellowship the elements inwards this set. Return aught 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 fellowship iterator over the set.
- NavigableSet<E> descendingSet() : It volition render contrary fellowship thought of laid elements.
- E first() : It volition render electrical current showtime chemical cistron from set.
- E floor(E e) : It volition furnish you lot greatest chemical cistron from the laid which is less than or equals to the specified element. If non constitute whatever hence 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 aught fi non constitute 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 final 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 constitute whatever hence it volition render null.
- E pollFirst() : It volition access together with withdraw showtime (lowest) chemical cistron from the set. If laid is empty hence it volition render null.
- E pollLast() : It volition access together with withdraw final (highest) chemical cistron from the set. If laid is empty hence it volition render null.
- boolean remove(Object o) : It volition withdraw 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 instance volition exhibit you lot usage of few TreeSet class's method's demo.
TreeSet Example :
package JAVAExamples; import java.util.Iterator; import java.util.TreeSet; populace shape 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: "); piece (iterator.hasNext()) { System.out.println(iterator.next() + " "); } // Get showtime chemical cistron from treeset. System.out.println("First chemical cistron inwards treeset is : " + ts.first()); // Get final 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 showtime chemical cistron from treeset. ts.pollFirst(); System.out.println("TreeSet elements subsequently pollFirst : " + ts); // Remove final chemical cistron from treeset. ts.pollLast(); System.out.println("TreeSet elements subsequently pollLast : " + ts); // Get tailset exclusive fromElement. System.out.println("tail laid from 69 is : " + ts.tailSet(69, false)); // Get subset exclusive fromElement together with toElement. System.out.println("subset laid from ix together with 76 exclusive is : "+ ts.subSet(9, false, 76, false)); // Get subset inclusive fromElement together with toElement. System.out.println("subset laid from ix together with 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 subsequently pollFirst : [5, 9, 15, 42, 52, 69, 76, 79, 93, 334] TreeSet elements subsequently pollLast : [5, 9, 15, 42, 52, 69, 76, 79, 93] tail laid from 69 is : [76, 79, 93] subset laid from ix together with 76 exclusive is : [15, 42, 52, 69] subset laid from ix together with 76 inclusive is : [9, 15, 42, 52, 69, 76]
<< PREVIOUS || NEXT >>