Check out a free preview of the full Java Fundamentals course

The "Queue" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates how to define a queue, a collection of elements that are processed in the order in which they are added. When a new item is added to a queue, it is added to the end of the array.

Preview
Close

Transcript from the "Queue" Lesson

[00:00:00]
>> The next one we'll talk about is a queue. So a queue is a collection of ordered elements. And it's typically used to hold items that are going to be processed in some way. So an example use case would be people in a checkout line at a supermarket, right?

[00:00:23]
So people who are new to the queue, they're added at the end of the line. And the processing of the queue is handled from the beginning to the end, right? This type of processing is known as first in, first out. So we are going to make the fruit as a queue.

[00:00:52]
So let's add a call to queueDemo here. We'll say public static void queueDemo. We'll use queue, As the type. And we will instantiate this as a linked list. So let's go ahead, I'm going to be a little lazy. I'm just gonna copy these fruit, apple, lemon, banana and orange.

[00:01:42]
And now we have some fruit added to the queue. Let's go ahead and print this out To make sure they're in order. So yep, the order that I added them that's the order that they stay in, all right? Queues do allow for duplicate elements. So if I said I wanna add another lemon, And I ran this then I have two lemons.

[00:02:25]
What's interesting with queues is let's say that with the list when we wanted to remove an element from the list we had to specify either the index or the object. For a queue, again, it's first-in-first-out. So if I say remove, I do have ways that I can remove a specific object.

[00:02:47]
But if I call remove I can call it with out a argument. And this is going to remove the very first thing that is in line. That is known as the head of the line. The last element would be known as the tail. Now, usually, if you remove something from a queue, again, queues are for processing the data.

[00:03:15]
So if I remove something from a queue, it's because maybe I want to do something with that object and get it out of the queue, right? So, this remove is not gonna return a void, most removes would just return void, but this will actually return you the object back.

[00:03:34]
So, I'm going to use var, here, [LAUGH] I don't feel like we've been giving that enough love. So we're gonna call this removed. And I think that that will, It'll remove the item from the queue, but it will store that object there. And so I can then like print out what was removed or do whatever I want with that object.

[00:04:05]
So let's just say removed and then say, + the removed object there. So I'm gonna run that. You see they removed the apple, because the apple was what? The head of the queue. All right, I don't wanna remove it, I wanna know who's next in line, but I'm not ready quite to remove it.

[00:04:45]
You can do that as well. So to do that, we can say, and let's put this in a print statement. And we'll say head of queue. And we can call fruits.peek. So peek will allow me to see what object is there. It'll actually, will it return this object?

[00:05:12]
Yeah, it'll return the object back to me. And, But it won't remove it from the queue, all right? So we removed the first element, which was apple, and it's telling us the next head of the queue would be limited. And then we printed it out and you see that lemon is still actually in the queue, okay?

[00:05:40]
Questions on queues?
>> What type did you make this queue?
>> I instantiated it as a linked list.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now