TreeSet is a widespread Java implementation of the SortedSet interface that employs a Tree for storage. Whether an explicit comparator is provided, a set maintains the ordering of the components using their natural ordering. If the Set interface is to be correctly implemented, this must be compatible with equals.
public class TreeSet<E>extends AbstractSet<E>implements NavigableSet<E>, Cloneable, Serializable
The following are key details of the Java TreeSet:
1)TreeSet(): This constructor creates an empty TreeSet object in which elements are stored in their default natural sorting order.
Syntax:
TreeSet tobj = new TreeSet();
2)TreeSet(Comparator): This constructor is used to create an empty TreeSet object whose elements will require an external sorting order specification.
Syntax:
TreeSet tobj = new TreeSet(Comparator com);
3)TreeSet(Collection): This constructor is used to create a TreeSet object that contains all the elements from the specified collection, with the elements stored in the default natural sorting order. In summary, this constructor is used when converting a Collection object to a TreeSet object.
Syntax:
TreeSet tobj = new TreeSet(Collection col);
4)TreeSet(SortedSet): This constructor is used to create a TreeSet object that contains all the elements from the specified sortedset, with the elements stored in the default natural sorting order. In a nutshell, this constructor is used to transform a SortedSet object into a TreeSet object.
Syntax:
TreeSet tobj = new TreeSet(SortedSet ss);
Method | Description |
---|---|
boolean add(E e) | If the supplied element does not already exist in this set, it is added. |
boolean addAll(Collection<? extends E> c) | It adds all of the elements from the specified collection to this set. |
E ceiling(E e) | It returns the equal or closest greatest element from the set to the supplied element, or null if no such element exists. |
Comparator<? super E> comparator() | It returns a comparator that sorts the elements. |
Iterator descendingIterator() | It’s used to loop through the components in decreasing order. |
NavigableSet descendingSet() | It returns the elements in reverse order. |
E floor(E e) | It returns the equal or closest least element from the set to the supplied element, or null if no such element exists. |
SortedSet headSet(E toElement) | It returns the element group that is less than the supplied element. |
NavigableSet headSet(E toElement, boolean inclusive) | It returns the set of elements that are smaller than or equal to the supplied element (if inclusive is true). |
E higher(E e) | It returns the closest greatest element from the set to the supplied element, or null if no such element exists. |
Iterator iterator() | It is used to iterate the elements in ascending order. |
E lower(E e) | It returns the set’s nearest least element to the supplied element, or null if no such element exists. |
E pollFirst() | It is used to retrieve and remove the lowest(first) element. |
E pollLast() | It is used to retrieve and remove the highest(last) element. |
Spliterator spliterator() | It is used to create a late-binding and fail-fast spliterator over the elements. |
NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) | It returns a set of elements that lie between the given range. |
SortedSet subSet(E fromElement, E toElement)) | It returns a list of elements that fall inside the supplied range, including fromElement but excluding toElement. |
SortedSet tailSet(E fromElement) | It returns a set of elements that are greater than or equal to the specified element. |
NavigableSet tailSet(E fromElement, boolean inclusive) | It produces a set of elements that are bigger than or equal to the supplied element (if inclusive is true). |
boolean contains(Object o) | If this set contains the specified element, it returns true. |
boolean isEmpty() | It returns true if this set contains no elements. |
boolean remove(Object o) | It is used to remove the specified element from this set if it is present. |
void clear() | It is used to remove all of the elements from this set. |
Object clone() | It returns a shallow copy of this TreeSet instance. |
E first() | It returns the first (lowest) element currently in this sorted set. |
E last() | It returns the last (highest) element currently in this sorted set. |
int size() | It returns the number of elements in this set. |
import java.util.*;
// Main class
class Coderz{
// Main driver method
public static void main(String[] args)
{
// Creating a NavigableSet object with
// reference to TreeSet class
NavigableSet<String> tobj = new TreeSet<>();
// Elements are added using add() method
tobj.add("COding");
tobj.add("at");
tobj.add("Coderz");
// Printing the elements inside the TreeSet object
System.out.println("Tree Set is " + tobj);
String check = "at";
// Check if the above string exists in
// the treeset or not
System.out.println("Contains " + check + " "
+ tobj.contains(check));
// Print the first element in
// the TreeSet
System.out.println("First Value " + tobj.first());
// Print the last element in
// the TreeSet
System.out.println("Last Value " + tobj.last());
String val = "at";
// Find the values just greater
// and smaller than the above string
System.out.println("Higher " + tobj.higher(val));
System.out.println("Lower " + tobj.lower(val));
}
}
Tree Set is [COding, Coderz, at]
Contains at true
First Value COding
Last Value at
Higher null
Lower Coderz
import java.util.*;
class Coderz {
// Main driver method
public static void main(String[] args)
{
// Creating an object of NavigableSet
// with reference to TreeSet class
// Declaring object of string type
NavigableSet<String> tobj = new TreeSet<>();
// Elements are added
// using add() method
tobj.add("Coding");
tobj.add("at");
tobj.add("Coderz");
tobj.add("X");
tobj.add("Y");
tobj.add("Z");
// Print and display initial elements of TreeSet
System.out.println("Initial TreeSet " + tobj);
// Removing a specific existing element inserted
// above
tobj.remove("Y");
// Printing the updated TreeSet
System.out.println("After removing element " + tobj);
// Now removing the first element
// using pollFirst() method
tobj.pollFirst();
// Again printing the updated TreeSet
System.out.println("After removing first " + tobj);
// Removing the last element
// using pollLast() method
tobj.pollLast();
// Lastly printing the elements of TreeSet remaining
// to figure out pollLast() method
System.out.println("After removing last " + tobj);
}
}
Initial TreeSet [Coderz, Coding, X, Y, Z, at]
After removing element [Coderz, Coding, X, Z, at]
After removing first [Coding, X, Z, at]
After removing last [Coding, X, Z]
import java.util.*;
// Main class
class Coderz {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Set with reference to
// TreeSet class
Set<String> tobj = new TreeSet<>();
// Adding elements in above object
tobj.add("Coding");
tobj.add("at");
tobj.add("Coderz");
tobj.add("X");
tobj.add("Y");
tobj.add("Z");
// Now we will be using for each loop in order
// to iterate through the TreeSet
for (String value : tobj)
// Printing the values inside the object
System.out.print(value + ", ");
System.out.println();
}
}
Coderz, Coding, X, Y, Z, at,
Note: also read about the Java ArrayList in Collection Framework
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…