The TreeSet class provides four constructors broken into two
sets.The first two constructors creates empty trees sets:
public TreeSet()
public TreeSet(Comparator comp)
In order to maintain an ordering, elements added to a tree
set must provide some way for the tree to order them. If the elements implement
the Comparable interface, the first constructor is sufficient. If, however, the
objects aren’t comparable or you don’t like the default ordering provided, you
can pass along a custom Comparator to the constructor that will be used to keep
elements ordered. Once the TreeSet is created, you cannot change the
comparator.
Note: As HashSet relying on a internal structure of
HashMap, the TreeSet relies on a TreeMap internally.
The other two constructors are copy constructors, copying
all elements from one collection into another:
public TreeSet (Collection c)
public TreeSet (SortedSet set)
If the other collection is a SortedSet, the TreeSet is able
to perform some optimizations while adding elements. It also retains the
original set’s comparator.
Happy Learning.
No comments:
Post a Comment