Java Essentials - The queue in java
24K views
Oct 24, 2024
Java Essentials - The queue in java
View Video Transcript
0:00
The Q in Java. So we know that Q is one data structure which supports the first in, first out and the last in, last out data structure
0:11
Q is a subclass of the class that is the link list. So let us go for some further discussion on this queue
0:20
The Q in Java collection. The Q is a subclass of the link list class. To insert element into Q, we can use
0:29
the add method of the linkless class and also to remove or delete some element from the queue
0:36
we can use the remove method accordingly but q is also having a separate some dedicated methods
0:43
defined within the q class so q also has the method that is a pick method it is used to return
0:50
the first element of the q not deleting that one so these are the different concept we have
0:55
shared so now let us go for one practical demonstration for your easy understanding In this program we have defined one object my Q This object is defined under the interface
1:08
You can find that in the help we are finding this one as interface. There is a Java.tutil.cue
1:14
So Java. .util is a package name and Q is the respective interface
1:19
And this queue has got instantiated using is equal to new link list, a constructor here
1:25
so how to add members to this Q object that is a my Q and this my Q will be holding integer that is a wrapper class objects as its member so to add that new elements we are going to go for new items we are going to go for this method add so using the method add we can add new items to this my queue that is a Q interface object so new my queue dot add 10 my queue dot add 40 my Q dot add 50 in this way
1:55
we have added five such elements we know that cube is a data structure which supports first in first out or last in last out that means we are we can think that this 10 is at the front end of the queue and 50
2:07
will be found at the rear end of the queue so now if I want to print the number of
2:12
elements we're having number of items we are having within this my queue there is a
2:17
queue interface object the method is size with the help of size you can print it
2:21
how many items you have added here five items so we are expecting that my q
2:26
size will print five here yes the skew size is five now we are going to pick
2:33
the first element that means in this case the first element will be red but it
2:37
will not get deleted from the queue and you can find that here we are getting
2:41
this first element as 10 because 10 is at the front end and 50 is at the rear
2:46
end so the first element will be at the front end so the value is 10 but the 10 has
2:51
not got deleted from the my queue object it is still there the item is still
2:56
there now using this while loop so my q dot is empty so when the queue will become empty then is empty will return true then not of true will become false then you can come out from this while
3:09
but when this is empty will be returning false that means the queue is still
3:14
having some items then not of false will become true the while will continue
3:19
doing the iteration so how to remove elements from the queue that is a my cue
3:24
dot remove so remove is the method with help of which you can eliminate
3:28
the element which will be found at that at the front end of the cube so the popped item is is this one also in case of pop type also item we can go for a removed item that will be better here
3:40
so the removed item is this one so let me go for the execution you can find that the remove item will be 10 then 40 then 30 then 20 then 20 then the
3:49
rearest object that is a rear most object there is a 50 is getting printed here in this way in this particular demonstration we have discussed
3:58
us how to define and deal with Q objects. Thanks for watching this video
#Java (Programming Language)