Java Essentials - Sets in java
3K views
Nov 18, 2024
Java Essentials - Sets in java Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited
View Video Transcript
0:00
Now let us discuss sets in Java
0:04
So set is nothing but one mathematical abstraction and in this particular collection duplicate data
0:11
are not allowed. So let us go for more discussion on these sets
0:15
So the sets in Java collection. A set is a collection that cannot contain duplicate elements and it models the mathematical
0:25
set abstraction. The set interface contains only methods inherited from the collection and it adds the restriction that duplicate elements will remain
0:36
prohibited. So that's why it is actually nothing but a mathematical set abstraction
0:43
So to have the better clarity on this topic, let us go for one practical demonstration
0:47
and in the demonstration using Java sample code will be discussing more on this set
0:53
In this demonstration, we shall discuss the set interface and its respective objects and
0:59
how to deal with them here we have defined two integer arrays list one and list two
1:05
and these are the set of data it is having you can find that some duplicate data
1:09
are also present we have defined one set interface object that is set one you
1:15
can find that this set is this interface I is written here is under the package
1:21
that is the Java dot util a collection that contains no duplicate elements and more formally set contents no pair of elements e1 and e2 such that u1 equals e2 and at most one null element as implied by its name this
1:39
interface models the mathematical set abstraction so here you can find that we have defined
1:45
two set class set interface objects as set 1 and set 2 is a new будто has set the
1:51
constructed has been called here so now we are having already we're having to
1:55
integer arrays and now we are populating this set one and set two adding
2:00
this items picking up from this integer areas at first from the list one to set
2:06
one and then from list two to set two so here we have used the for each loop so
2:11
for item item item colon list one so each and every item will be picked up one at
2:17
a time each each and every integer data will be picked up one at a time
2:21
then that will get added with the set one and you can find that this set one is
2:26
actually the object under the interface set and here the set will hold the integer
2:33
that is a wrapper class objects so system dot out of print a length the first set
2:38
will be printed and the second set will be printed you can find that we're
2:42
having this first set has got printed so it has got printed in this way one two
2:47
and then it is having five then six then twelve you see the
2:51
duplicate data data have got removed there is no duplicate data here and they have got shorted automatically so now we are having this the second one that is a 12 4 3 2 4 15
3:03
you can find that it has become 2 3 4 12 and 15 here now we shall go for the set
3:10
operations the union of two sets we have defined one set interface object that is
3:16
the union is equal to new has set which will be containing the typecasting has been done
3:22
as integer upper class object and then set 1 so using the set 1 members this union
3:29
will get populated and now this union is having all the items from set 1 and then union
3:36
dot add all set 2 so add items of the set 2 with the set 1 set 1 items which are residing
3:43
in this union that is a interface set object so now if I go on printing the current
3:49
value of union then I'm getting this one you can find that all the data items are unique all the data items are distinct so there
3:58
is no repetition or data items and the items have been printed in the shorted
4:02
form so now we shall go for the intersection now we shall go for the intersection so
4:08
we have defined the one here one set interface object that is the intersection
4:13
which will hold this integer upper class objects as its items and intersection
4:18
dot written all just remember this method that method is written all and that is useful to do the intersection of two sets and here add all that was used to do the union of two sets here so written all set two so now
4:32
this intersection current content has got printed you can find that it has got
4:37
printed as 2 and 12 because 2 and 12 are the common elements between this set 1
4:42
and set 2 now we shall go for the difference so now the method will be a
4:49
remove all that means at first the difference has got instantiated with the items from set one and then diff dot
4:56
remove all set two so remove common elements from the two sets and in this way
5:02
we're going to get the updated content and the reduced content of diff okay so
5:09
now you see this is my set one and this my set two here we have printed them
5:14
earlier so now if you go for set 1 minus set 2 I'm remaining with this one 5 and
5:21
six because in between these two sets only two is common so two is common and then
5:28
this one five six 12 is also common here so that's why one five six is the only the outcome of the
5:35
difference set one minus set two in this way in this demonstration you have discussed a lot on this set
5:41
interface and how to deal with its methods and how to define the respective objects how to
5:47
initialize them and you have different we have discussed that one into detail
5:51
details with some sample coding. Thanks for watching this video
#Computers & Electronics
#Java (Programming Language)
#Programming