1
00:00:00,000 --> 00:00:02,130
Hi, there. Today in this session

2
00:00:02,130 --> 00:00:03,930
I will talk about TreeSet,

3
00:00:03,930 --> 00:00:07,185
which is one of the most
important implementation of

4
00:00:07,185 --> 00:00:09,450
the sorted set interface in

5
00:00:09,450 --> 00:00:13,500
Java that uses a
tree for storage.

6
00:00:13,500 --> 00:00:16,770
Basically it inherits
abstract set class

7
00:00:16,770 --> 00:00:19,845
and implements the
navigable set interface.

8
00:00:19,845 --> 00:00:21,030
Whenever we work with

9
00:00:21,030 --> 00:00:23,250
the navigable set
interface methods,

10
00:00:23,250 --> 00:00:26,115
we need to instantiate
the TreeSet only.

11
00:00:26,115 --> 00:00:28,245
The ordering of the elements

12
00:00:28,245 --> 00:00:30,630
is maintained by
a set using their

13
00:00:30,630 --> 00:00:33,330
natural ordering whether or

14
00:00:33,330 --> 00:00:35,970
not an explicit
competitor is provided.

15
00:00:35,970 --> 00:00:38,525
This must be
consistent with equal,

16
00:00:38,525 --> 00:00:42,565
if it is correctly implemented
by the set interface.

17
00:00:42,565 --> 00:00:45,440
It's access and retrieval
times are quick,

18
00:00:45,440 --> 00:00:48,260
fast because it's
implementation is free.

19
00:00:48,260 --> 00:00:50,810
TreeSet is being implemented

20
00:00:50,810 --> 00:00:53,045
using the binary search tree,

21
00:00:53,045 --> 00:00:56,510
which is self-balancing
just like a red black tree.

22
00:00:56,510 --> 00:00:59,030
Therefore, operations
such as search, remove,

23
00:00:59,030 --> 00:01:02,480
and add, consume, they
go login and time.

24
00:01:02,480 --> 00:01:03,980
The reason behind this is

25
00:01:03,980 --> 00:01:06,095
there in the
self-balancing tree.

26
00:01:06,095 --> 00:01:08,630
If there is to ensure
the three hype never

27
00:01:08,630 --> 00:01:11,810
exceed by the log on for all
the mentioned operations

28
00:01:11,810 --> 00:01:15,920
because it's half of the
entire tree to be hydrated

29
00:01:15,920 --> 00:01:17,780
and objectives set to be

30
00:01:17,780 --> 00:01:20,930
hydrated when it comes to
the part of the tree only.

31
00:01:20,930 --> 00:01:22,040
Therefore, it is one of

32
00:01:22,040 --> 00:01:24,560
the efficient data
structure in order to keep

33
00:01:24,560 --> 00:01:26,090
the large data that is

34
00:01:26,090 --> 00:01:28,550
sorted and also do
operations on it.

35
00:01:28,550 --> 00:01:31,520
These are some specific
methods that you can use

36
00:01:31,520 --> 00:01:34,310
with a TreeSet along
with whatever methods

37
00:01:34,310 --> 00:01:37,700
are valuable in its
extent implemented

38
00:01:37,700 --> 00:01:42,425
or extended interfaces and
classes you can come across.

39
00:01:42,425 --> 00:01:46,730
Let's try to implement it
practically with an example.

40
00:01:46,730 --> 00:01:52,530
Here I'm creating a set
of string elements.

41
00:01:58,300 --> 00:02:02,520
We have the classes TreeSet.

42
00:02:05,470 --> 00:02:15,060
Set1.add, set.add, let's say A,

43
00:02:16,090 --> 00:02:20,840
set.add, let's say B,

44
00:02:20,840 --> 00:02:29,375
set.add let's say C.
Here we says set.add,

45
00:02:29,375 --> 00:02:32,030
let's say C one more time and

46
00:02:32,030 --> 00:02:35,730
now let's try to
print our TreeSet.

47
00:02:36,230 --> 00:02:39,800
You can see that duplicacy is

48
00:02:39,800 --> 00:02:43,190
removed and the natural
order is maintained.

49
00:02:43,190 --> 00:02:47,825
Moreover, if you would like
to check for the contains,

50
00:02:47,825 --> 00:02:54,165
just go and check set.contains,

51
00:02:54,165 --> 00:02:57,365
let me check whether
C contains or not.

52
00:02:57,365 --> 00:03:00,830
This time I'm checking
whether D contains or not,

53
00:03:00,830 --> 00:03:05,345
so it should return false
because it does not contains.

54
00:03:05,345 --> 00:03:08,420
Now from the tree
I can traverse,

55
00:03:08,420 --> 00:03:11,430
I can check set.first.

56
00:03:12,400 --> 00:03:16,360
I can see that it's
not coming up.

57
00:03:16,360 --> 00:03:24,545
You can see that first and
last is not coming up.

58
00:03:24,545 --> 00:03:28,540
If you will change
it to navigate set.

59
00:03:28,540 --> 00:03:31,410
Now, all these methods
could be available.

60
00:03:31,410 --> 00:03:34,160
Depends by which
particular operation

61
00:03:34,160 --> 00:03:37,070
you want it to come up and
what's your requirement.

62
00:03:37,070 --> 00:03:39,485
You wanted to navigate
or you wanted

63
00:03:39,485 --> 00:03:41,945
to go for the set
properties, as well.

64
00:03:41,945 --> 00:03:45,435
You can also get sys out

65
00:03:45,435 --> 00:03:50,400
set.higher then a
particular value,

66
00:03:50,400 --> 00:03:51,930
let's say D higher than

67
00:03:51,930 --> 00:04:01,860
B. I can go for sec.lower
than a particular value,

68
00:04:01,860 --> 00:04:05,040
let's say v. Let's see
what it gets printed.

69
00:04:05,040 --> 00:04:07,220
You can see that higher than B

70
00:04:07,220 --> 00:04:09,700
is C and lower than B is A.

71
00:04:09,700 --> 00:04:13,175
This is how the set
operations can be evoked.

72
00:04:13,175 --> 00:04:15,965
If we would like to
go for navigable set,

73
00:04:15,965 --> 00:04:18,800
you can also remove the
element with the help

74
00:04:18,800 --> 00:04:27,345
of set.pollfirst
and set.polllast.

75
00:04:27,345 --> 00:04:33,310
It will tell you which element
is removed or pulled out.

76
00:04:38,300 --> 00:04:41,630
Internally
stringbuffer also uses

77
00:04:41,630 --> 00:04:43,985
the TreeSet because the moment

78
00:04:43,985 --> 00:04:46,325
the TreeSet is to
be implemented,

79
00:04:46,325 --> 00:04:48,470
it will give me
the implementation

80
00:04:48,470 --> 00:04:51,710
for duplicate values
to not be allowed,

81
00:04:51,710 --> 00:04:53,630
and that's what the
StringBuffer see

82
00:04:53,630 --> 00:04:55,715
is that in the natural
order is maintained,

83
00:04:55,715 --> 00:04:59,160
that's the feature of
the presets says that.

84
00:04:59,160 --> 00:05:02,600
I hope you can understand
where exactly needs to be

85
00:05:02,600 --> 00:05:04,040
implemented this preset in

86
00:05:04,040 --> 00:05:06,455
the real time after
this discussion.

87
00:05:06,455 --> 00:05:09,720
Until next time. See you soon.
