Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Data Structures!
You have completed Java Data Structures!
Preview
Let's explore the `java.util.Queue` interface and learn how to poll for the next item to be processed.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Alright, we're now ready
to get familiarized with the final data
0:01
structure interface I wanted to make sure
you picked up from this course.
0:04
And that is the queue interface.
0:09
Queues are used when you want to store
elements prior to processing.
0:11
They provide convenient methods
0:15
for grabbing the next element
and removing it from the list.
0:16
This is called polling.
0:20
By default, queues
work in FIFO, first in, first out.
0:22
I put stuff in the queue that I want
something else to process when it's ready.
0:26
And that program does so
by calling the pull method on the queue.
0:30
If there is an element,
it gets removed and returned.
0:34
If not, it returns a null.
0:37
This is used a lot in loops.
0:39
While there's a next element in the queue
to process, process it.
0:41
Let's allow our users to choose a song
to add to a queue
0:45
and add a way for our karaoke jockey
to poll for the next song.
0:49
Okay, before we
0:54
get cooking, I noticed something
a little off with what I did last time.
0:55
I forgot to add the heading
when we were prompting for songs.
0:59
So, you know, before this prompt
for index happens, all of a sudden
1:02
there's this blast of songs
without any message there.
1:05
So let's add one there.
1:08
System.out.printf
1:10
Available songs for %s
1:16
%n and
1:21
artist.
1:24
That should look much better. See?
1:25
We all make mistakes.
1:27
It's part of the learning process,
so I'm totally cool with it.
1:28
Let's go break some more stuff.
1:31
Okay,
so let's go explore that queue interface.
1:33
Let's pop open the documentation,
1:36
and let's take a look
at some of its implementing classes.
1:38
So we have array deque.
1:41
Here's that linked list.
1:43
Let's jump into this array deque.
1:45
I think I remember them
talking about use this if you're not sure.
1:47
So this is a
1:51
resizable array implementation
of the deck interface.
1:52
Array decks have no capacity restrictions.
1:56
They can grow as they need,
not thread safe.
1:59
Sounds good. Let's use that.
2:01
So this has an add method
where you add the element
2:03
and it will return true if that worked.
2:06
It also has poll so it retrieves
and removes the head of the queue
2:11
represented by the deque. In other words,
the first element of this deque.
2:15
So a deque, as opposed to a cue
2:24
that we were talking
about, is a double-ended cue.
2:26
Right?
2:29
The name deque is short
for a double-ended cue.
2:29
Cool.
2:32
So let's go use it.
2:33
So first things first,
2:36
let's import it
so I don't forget like last time.
2:37
Karaoke machine, here we go.
2:40
So import
both the implementation and the interface.
2:42
And let's add a member variable for this.
2:59
We'll say private queue
3:04
called msong queue.
3:08
And here we'll initialize it.
3:13
Array deque, double ended queue,
3:19
and we want songs in it.
3:21
Cool.
3:25
Alright, so now that we have it,
let's take care of that to do
3:27
we left here in the choose method.
3:29
So to do add song to the queue.
3:31
So now we have the queue
and we know that we can add
3:34
the song.
3:36
Great!
3:42
Now let's show
the count in the prompt action,
3:43
so people know how long they'll be
waiting, right?
3:46
So there are songs available and %d
3:49
in the queue.
3:51
So let's add that extra msongqueue.size.
3:55
Cool.
4:05
I think we can now call this ticket here.
4:05
As a singer, I should be able to choose
a song and place it in the queue
4:08
so I have the opportunity to sing.
4:11
I think we can call that done-zo.
4:13
You know, while we're in here,
why don't we look at this KJ one?
4:16
As a karaoke jockey, or KJ,
I should be able to call out the next song
4:19
in the queue to sing
and the video should be available
4:24
so that karaoke occurs.
Let's do that while we're in here.
4:26
Okay so let's add a method down here.
4:30
Let's make it public
so anybody can call this one.
4:34
It doesn't need to return anything.
4:38
Call it playNext,
and it also doesn't need to take anything.
4:40
So let's go ahead and use that queue.
4:45
So the mSongQueue,
4:50
and we saw that there's
this thing called poll, right?
4:51
So what that will do is it's going to take
the next one that's available,
4:54
if there is one, and remove it
from the queue as well as return it.
4:57
But if it doesn't exist,
it's going to be null.
5:01
So just in case somebody says hey,
let's play the next one, and they didn't
5:04
look at the queue count,
we should tell them how
5:07
this is supposed to work, right?
5:09
Good error messages are always helpful.
5:10
So if song equals null, we'll say, sorry,
there are no songs in the queue.
5:13
Use choose from the menu to add some.
5:24
And so else,
if there is a song, let's go ahead
5:31
and show them the song.
5:34
We'll give them some space.
5:38
Open %s to hear %s by %s.
5:40
We'll do some new lines after that too.
5:45
So we're saying, open the video URL
5:49
to hear the song title
from the song artist.
5:51
Or at least in the style of, I guess.
5:54
Okay.
5:56
Finally,
6:01
let's add that to the menu
so that KJ can do his job here.
6:01
So let's copy and paste this add line.
6:04
Then we'll say play.
6:09
Play next song in the queue.
6:19
And let's go ahead and we'll add that
to our switch down here.
6:22
So if somebody does enter play,
6:27
we'll just call that method playNext,
and let's remember to break.
6:28
Alright, so let's go
6:35
ahead and kick this off.
6:36
Well, it looks I misspelled an import.
6:45
I bet you've all been yelling at me
through your screens, huh?
6:48
It happens to all of us.
Let me fix that. Aye,
6:51
yi, yi. Hmm.
6:59
Object cannot be converted to song.
7:09
Line 122.
7:12
That's where we're polling.
7:15
That looks right.
7:17
Maybe I declared it wrong.
7:18
Oh, here we go.
7:21
I forgot to put the generic type.
7:22
So we saw there
it said incompatible types.
7:24
When I declared it,
I forgot to say it was of type song.
7:27
So that is the message that you'll get
sometimes if you use these interfaces.
7:31
Glad this error happened, actually.
7:35
If you don't put that type
7:37
definition there, what happens
is it returns an object.
7:38
So here we go. Let's try that again.
7:41
There we go.
7:45
We got this thing working. Okay.
7:45
So we'll add.
7:48
This time I want to add a song by Level 42
7:49
called Lessons in Love.
7:52
And I'll just do example.com again.
7:58
Okay, so there we go.
8:02
Lessons in Love was added. Great song.
8:03
There's one songs
available and zero in the queue.
8:06
So I'm going to go ahead and choose.
8:09
I'll choose Level 42.
8:11
I want to choose lessons in love
8:13
and now we'll see that there are one song -
8:15
there are one songs available.
There we go with that bug again
8:18
and one in the queue.
So let's go ahead and play that.
8:22
So it says open this to hear yada
yada yada.
8:27
So if this were a real link,
I could highlight this
8:30
and right click and select go to
and it would open it up for me.
8:32
Cool and down here we can see that
it's been removed from the queue.
8:37
All right, so let's go close that ticket.
8:40
So we are doing good.
8:44
We're all done except for storing.
8:46
Let's figure out a way
to keep this information around.
8:47
Nice.
8:52
Now we know about the queue interface,
and that wraps up all the Java Collection
8:53
Framework interfaces that I wanted you
to get a chance to play with.
8:57
Our karaoke machine is just about ready
for its first party.
9:01
All we need now is a way to load it up
with some songs.
9:04
Let's go knock out that last ticket.
9:07
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up