Kuonyesha tu
0:03
S… Speaker 1 (10- The Queue Interface)
The next interface we're going to talk about is the queue interface.
0:06
S… Speaker 1 (10- The Queue Interface)
Similar to the list interface,
0:08
S… Speaker 1 (10- The Queue Interface)
it extends the collection interface,
0:10
S… Speaker 1 (10- The Queue Interface)
so every queue is a collection as well.
0:12
S… Speaker 1 (10- The Queue Interface)
We use queues in situations where we want to process jobs based
0:16
S… Speaker 1 (10- The Queue Interface)
on the order we receive them.
0:18
S… Speaker 1 (10- The Queue Interface)
For example,
0:19
S… Speaker 1 (10- The Queue Interface)
think of the printer at your organization.
0:21
S… Speaker 1 (10- The Queue Interface)
Your printer receives various jobs,
0:23
S… Speaker 1 (10- The Queue Interface)
puts them in a queue,
0:24
S… Speaker 1 (10- The Queue Interface)
and processes them one by one in the order it receives them.
0:28
S… Speaker 1 (10- The Queue Interface)
Here on the documentation you can see that the queue interface has these sub -interfaces
0:33
S… Speaker 1 (10- The Queue Interface)
like blocking deck,
0:34
S… Speaker 1 (10- The Queue Interface)
blocking queue,
0:35
S… Speaker 1 (10- The Queue Interface)
deck,
0:36
S… Speaker 1 (10- The Queue Interface)
and transfer queue.
0:37
S… Speaker 1 (10- The Queue Interface)
Again,
0:38
S… Speaker 1 (10- The Queue Interface)
these are for special purposes and chances are you're not going to use them for the most of
0:42
S… Speaker 1 (10- The Queue Interface)
your career.
0:42
S… Speaker 1 (10- The Queue Interface)
And here you can see various implementations of the queue interface.
0:46
S… Speaker 1 (10- The Queue Interface)
The two implementations you use most of the time are array deck and
0:50
S… Speaker 1 (10- The Queue Interface)
priority queue.
0:52
S… Speaker 1 (10- The Queue Interface)
Deck is short for double ended queue.
0:54
S… Speaker 1 (10- The Queue Interface)
It's a special type of queue that has two ends.
0:57
S… Speaker 1 (10- The Queue Interface)
So items can enter the queue from either end.
1:00
S… Speaker 1 (10- The Queue Interface)
This is different from the queues that we have in real life.
1:02
S… Speaker 1 (10- The Queue Interface)
For example,
1:03
S… Speaker 1 (10- The Queue Interface)
when we go in a queue for a concert or a movie,
1:06
S… Speaker 1 (10- The Queue Interface)
we always enter the queue from the back.
1:08
S… Speaker 1 (10- The Queue Interface)
With double ended queues or decks,
1:11
S… Speaker 1 (10- The Queue Interface)
items can enter from either end.
1:13
S… Speaker 1 (10- The Queue Interface)
Priority queue is another common implementation and again it's a
1:17
S… Speaker 1 (10- The Queue Interface)
special type of queue.
1:18
S… Speaker 1 (10- The Queue Interface)
It's a queue where each item gets a priority and this priority
1:22
S… Speaker 1 (10- The Queue Interface)
determines the position of this item in the queue.
1:25
S… Speaker 1 (10- The Queue Interface)
So items with a higher priority move to the front of the queue.
1:28
S… Speaker 1 (10- The Queue Interface)
A common application of this is the queue that your operating system uses to
1:32
S… Speaker 1 (10- The Queue Interface)
manage the processes.
1:33
S… Speaker 1 (10- The Queue Interface)
Some processes have a higher priority so they get more CPU time.
1:38
S… Speaker 1 (10- The Queue Interface)
So let's declare a variable of type queue of string
1:43
S… Speaker 1 (10- The Queue Interface)
We call it queue and set it to a new array deck.
1:47
S… Speaker 2 (10- The Queue Interface)
Now,
1:48
S… Speaker 1 (10- The Queue Interface)
by the way, I didn't specify this generic type argument over here because
1:52
S… Speaker 1 (10- The Queue Interface)
this is redundant.
1:53
S… Speaker 1 (10- The Queue Interface)
The Java compiler can look at what we have on the left side and
1:57
S… Speaker 1 (10- The Queue Interface)
figure out that this is an array deck of strings.
2:00
S… Speaker 1 (10- The Queue Interface)
This is the reason why a string is grayed out,
2:02
S… Speaker 1 (10- The Queue Interface)
so we can remove it.
2:04
S… Speaker 1 (10- The Queue Interface)
Now in this queue let's add a few items.
2:07
S… Speaker 1 (10- The Queue Interface)
Let's imagine that C entered the queue first.
2:10
S… Speaker 1 (10- The Queue Interface)
followed by A,
2:12
S… Speaker 1 (10- The Queue Interface)
and then B.
2:13
S… Speaker 1 (10- The Queue Interface)
So our queue looks like this.
2:15
S… Speaker 2 (10- The Queue Interface)
B,
2:15
S… Speaker 2 (10- The Queue Interface)
A,
2:17
S… Speaker 1 (10- The Queue Interface)
and C.
2:18
S… Speaker 1 (10- The Queue Interface)
So C is at the front of the queue.
2:20
S… Speaker 1 (10- The Queue Interface)
Now we have another method for adding an item to a queue,
2:24
S… Speaker 1 (10- The Queue Interface)
and that is offer.
2:25
S… Speaker 1 (10- The Queue Interface)
So queue .offer,
2:27
S… Speaker 1 (10- The Queue Interface)
we can now offer D,
2:29
S… Speaker 1 (10- The Queue Interface)
so D will be at the end or the rear
2:33
S… Speaker 1 (10- The Queue Interface)
of the queue.
2:34
S… Speaker 1 (10- The Queue Interface)
Now what is the difference between add and offer?
2:37
S… Speaker 1 (10- The Queue Interface)
The difference depends on the implementation of the queue that we are using.
2:40
S… Speaker 1 (10- The Queue Interface)
In case of an array deck,
2:42
S… Speaker 1 (10- The Queue Interface)
there is no difference,
2:43
S… Speaker 1 (10- The Queue Interface)
but in some implementations,
2:45
S… Speaker 1 (10- The Queue Interface)
the queue might have a limited size.
2:46
S… Speaker 1 (10- The Queue Interface)
In those cases,
2:48
S… Speaker 1 (10- The Queue Interface)
if the queue gets full,
2:49
S… Speaker 1 (10- The Queue Interface)
the add method throws an exception,
2:51
S… Speaker 1 (10- The Queue Interface)
whereas the offer method returns false.
2:54
S… Speaker 1 (10- The Queue Interface)
So it's kind of less aggressive.
2:55
S… Speaker 1 (10- The Queue Interface)
Now let me delete this line.
2:58
S… Speaker 1 (10- The Queue Interface)
So our queue looks like this.
3:00
S… Speaker 1 (10- The Queue Interface)
Now we can get the item at the front of the queue by calling the
3:05
S… Speaker 1 (10- The Queue Interface)
peak method.
3:06
S… Speaker 1 (10- The Queue Interface)
So this will return the item at the front.
3:09
S… Speaker 1 (10- The Queue Interface)
We can print this.
3:10
S… Speaker 1 (10- The Queue Interface)
So at the front we have C.
3:13
S… Speaker 1 (10- The Queue Interface)
So now we should see the C.
3:16
S… Speaker 2 (10- The Queue Interface)
Beautiful.
3:17
S… Speaker 1 (10- The Queue Interface)
We have another similar method that is element.
3:21
S… Speaker 1 (10- The Queue Interface)
The difference between peak and element is that peak returns null
3:25
S… Speaker 1 (10- The Queue Interface)
if the queue is empty,
3:26
S… Speaker 1 (10- The Queue Interface)
whereas element throws an exception.
3:28
S… Speaker 1 (10- The Queue Interface)
Let me show you.
3:29
S… Speaker 1 (10- The Queue Interface)
So I'm going to comment out these few lines.
3:31
S… Speaker 1 (10- The Queue Interface)
Now our queue is empty.
3:33
S… Speaker 1 (10- The Queue Interface)
So the element method
3:37
S… Speaker 1 (10- The Queue Interface)
throws an exception.
3:39
S… Speaker 1 (10- The Queue Interface)
In contrast,
3:40
S… Speaker 1 (10- The Queue Interface)
if we call the peak method,
3:43
S… Speaker 1 (10- The Queue Interface)
we'll get null.
3:44
S… Speaker 2 (10- The Queue Interface)
There you go.
3:46
S… Speaker 2 (10- The Queue Interface)
Now,
3:48
S… Speaker 1 (10- The Queue Interface)
let me bring this back.
3:50
S… Speaker 2 (10- The Queue Interface)
Alright.
3:51
S… Speaker 1 (10- The Queue Interface)
Now,
3:52
S… Speaker 1 (10- The Queue Interface)
to remove the item at the front of the queue,
3:53
S… Speaker 1 (10- The Queue Interface)
we call the remove method.
3:56
S… Speaker 1 (10- The Queue Interface)
This will remove the item at the front and return it.
3:59
S… Speaker 1 (10- The Queue Interface)
So now,
4:00
S… Speaker 1 (10- The Queue Interface)
when we run this program,
4:02
S… Speaker 1 (10- The Queue Interface)
we get the C,
4:05
S… Speaker 1 (10- The Queue Interface)
and if we print the entire queue,
4:09
S… Speaker 1 (10- The Queue Interface)
you can see,
4:10
S… Speaker 1 (10- The Queue Interface)
that the C is gone and our queue only has A and B.
4:14
S… Speaker 1 (10- The Queue Interface)
We have a similar method called pole.
4:17
S… Speaker 1 (10- The Queue Interface)
The difference is that pole returns null if the queue is empty,
4:21
S… Speaker 1 (10- The Queue Interface)
whereas remove throws an exception.
4:23
S… Speaker 1 (10- The Queue Interface)
So once again,
4:25
S… Speaker 1 (10- The Queue Interface)
let me show you,
4:26
S… Speaker 1 (10- The Queue Interface)
I'm going to comment out these few lines.
4:27
S… Speaker 1 (10- The Queue Interface)
So pole is going to return null.
4:30
S… Speaker 2 (10- The Queue Interface)
There you go.
4:33
S… Speaker 1 (10- The Queue Interface)
Whereas if we call the remove method,
4:36
S… Speaker 1 (10- The Queue Interface)
we get an exception.
4:37
S… Speaker 2 (10- The Queue Interface)
There you go.
4:41
S… Speaker 1 (10- The Queue Interface)
So this is how we can use cues. In the next video we're going to talk about sets.

Nakala hii ilitokezwa na AI (utambuaji wa usemi wa kiaya). Inaweza kuwa na makosa Équipe dhidi ya sauti ya awali kwa utumizi wa kuchambua. Sera ya AI

❤️ Love STT.ai? Tell your friends!
Muhtasari
Bonyeza muhtasari ili kutokeza muhtasari wa AI juu ya nakala hii.
Kutoa muhtasari...
Uliza Maswali Kuhusu Mpito Huu
Uliza jambo lolote kuhusu nakala hii ya kitabu KULEA ile nitakayopata sehemu zinazofaa na majibu.