Hi, there. Today in this session
I will talk about TreeSet,
which is one of the most important implementation of
the sorted set interface in
Java that uses a tree for storage.
Basically it inherits abstract set class
and implements the navigable set interface.
Whenever we work with
the navigable set interface methods,
we need to instantiate the TreeSet only.
The ordering of the elements
is maintained by a set using their
natural ordering whether or
not an explicit competitor is provided.
This must be consistent with equal,
if it is correctly implemented by the set interface.
It's access and retrieval times are quick,
fast because it's implementation is free.
TreeSet is being implemented
using the binary search tree,
which is self-balancing just like a red black tree.
Therefore, operations such as search, remove,
and add, consume, they go login and time.
The reason behind this is
there in the self-balancing tree.
If there is to ensure the three hype never
exceed by the log on for all the mentioned operations
because it's half of the entire tree to be hydrated
and objectives set to be
hydrated when it comes to the part of the tree only.
Therefore, it is one of
the efficient data structure in order to keep
the large data that is
sorted and also do operations on it.
These are some specific methods that you can use
with a TreeSet along with whatever methods
are valuable in its extent implemented
or extended interfaces and classes you can come across.
Let's try to implement it practically with an example.
Here I'm creating a set of string elements.
We have the classes TreeSet.
Set1.add, set.add, let's say A,
set.add, let's say B,
set.add let's say C. Here we says set.add,
let's say C one more time and
now let's try to print our TreeSet.
You can see that duplicacy is
removed and the natural order is maintained.
Moreover, if you would like to check for the contains,
just go and check set.contains,
let me check whether C contains or not.
This time I'm checking whether D contains or not,
so it should return false because it does not contains.
Now from the tree I can traverse,
I can check set.first.
I can see that it's not coming up.
You can see that first and last is not coming up.
If you will change it to navigate set.
Now, all these methods could be available.
Depends by which particular operation
you want it to come up and what's your requirement.
You wanted to navigate or you wanted
to go for the set properties, as well.
You can also get sys out
set.higher then a particular value,
let's say D higher than
B. I can go for sec.lower than a particular value,
let's say v. Let's see what it gets printed.
You can see that higher than B
is C and lower than B is A.
This is how the set operations can be evoked.
If we would like to go for navigable set,
you can also remove the element with the help
of set.pollfirst and set.polllast.
It will tell you which element is removed or pulled out.
Internally stringbuffer also uses
the TreeSet because the moment
the TreeSet is to be implemented,
it will give me the implementation
for duplicate values to not be allowed,
and that's what the StringBuffer see
is that in the natural order is maintained,
that's the feature of the presets says that.
I hope you can understand where exactly needs to be
implemented this preset in
the real time after this discussion.
Until next time. See you soon.