1
00:00:00,540 --> 00:00:02,240
Hello everyone. In this session

2
00:00:02,240 --> 00:00:03,250
let us look at the TreeSet.

3
00:00:03,870 --> 00:00:05,730
TreeSet is an important implementation

4
00:00:05,730 --> 00:00:07,460
of SortedSet interface.

5
00:00:07,840 --> 00:00:11,230
It is a sorted type of set, very

6
00:00:11,230 --> 00:00:13,100
similar to HashSet but the difference

7
00:00:13,100 --> 00:00:15,960
is, it is an ordered collection and elements

8
00:00:15,970 --> 00:00:19,380
are sorted in the ascending order by default.

9
00:00:19,420 --> 00:00:21,740
So, inside a TreeSet, when we add

10
00:00:21,740 --> 00:00:23,810
the elements, those are automatically

11
00:00:23,810 --> 00:00:25,570
sorted in the ascending order.

12
00:00:25,580 --> 00:00:27,240
So, let's create an object of TreeSet.

13
00:00:27,390 --> 00:00:31,100
Let us use the set interface to create the object,

14
00:00:31,110 --> 00:00:36,640
say <string> set1 = new TreeSet.

15
00:00:38,240 --> 00:00:41,360
Import the TreeSet and import the set interface.

16
00:00:42,040 --> 00:00:43,780
Now, to create the object of a set,

17
00:00:44,020 --> 00:00:46,240
you can use the var keyword if you're

18
00:00:46,310 --> 00:00:49,350
using Java 10 or you can use the particular

19
00:00:49,350 --> 00:00:53,100
reference type like TreeSet or you can use the set

20
00:00:53,100 --> 00:00:55,250
interface because it is the parent interface.

21
00:00:55,740 --> 00:00:57,070
So, you can choose any of the ways.

22
00:00:57,300 --> 00:00:59,240
Now, let us add some elements to the set.

23
00:00:59,240 --> 00:01:02,360
Let us say I want to add some names.

24
00:01:03,140 --> 00:01:05,060
So, we have added four items, Rob,

25
00:01:05,060 --> 00:01:07,040
Bob, Andy, and Charls.

26
00:01:07,040 --> 00:01:10,650
And let us simply display the set over here.

27
00:01:11,540 --> 00:01:14,350
So, in this case we will see four elements

28
00:01:14,350 --> 00:01:16,560
in the list, but those will be sorted

29
00:01:16,570 --> 00:01:18,360
automatically in the ascending order.

30
00:01:19,040 --> 00:01:20,280
So, when we look at the output,

31
00:01:20,550 --> 00:01:22,330
the set is automatically sorted.

32
00:01:22,330 --> 00:01:23,850
So, A, B, C, and R.

33
00:01:25,140 --> 00:01:26,870
Let's add some other elements.

34
00:01:26,870 --> 00:01:30,720
Let's create another set , say set2 =

35
00:01:31,050 --> 00:01:35,250
to new TreeSet<Integer>( ); integer.

36
00:01:35,640 --> 00:01:39,450
and let's quickly add some integer values.

37
00:01:39,840 --> 00:01:41,000
So, we have added some random

38
00:01:41,020 --> 00:01:43,040
integer values and let us also

39
00:01:43,040 --> 00:01:44,840
display the set2.

40
00:01:45,640 --> 00:01:47,790
So, in this case we can expect the set2

41
00:01:47,790 --> 00:01:50,290
also get sorted in the ascending

42
00:01:50,290 --> 00:01:51,490
order by default.

43
00:01:51,500 --> 00:01:53,590
So, that is the main difference in between

44
00:01:53,820 --> 00:01:56,310
the HashSet and TreeSet.

45
00:01:56,690 --> 00:01:58,690
In case of HashSet, it is not an ordered

46
00:01:58,890 --> 00:02:00,290
collection, in case of TreeSet,

47
00:02:00,420 --> 00:02:03,540
it is an ordered collection, but it is ordered

48
00:02:03,550 --> 00:02:05,560
in the sorted fashion.

49
00:02:05,570 --> 00:02:07,110
It sorts the elements automatically

50
00:02:07,310 --> 00:02:09,240
in the ascending order. Other than that,

51
00:02:09,250 --> 00:02:10,810
all methods are same,

52
00:02:11,009 --> 00:02:14,090
add, remove, clear, contents, very same to

53
00:02:14,169 --> 00:02:15,700
what we have seen in the HashSet,

54
00:02:16,300 --> 00:02:20,180
and union and intersection methods are similar,

55
00:02:20,620 --> 00:02:22,660
converting to list is also similar.

56
00:02:23,640 --> 00:02:25,930
Okay, so this is about the HashSet,

57
00:02:26,160 --> 00:02:28,540
LinkedHashSet, and TreeSet in Java, and there's

58
00:02:28,540 --> 00:02:30,660
also an interview question. Thank you.

