Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRobert Angelo Valencia
Java Web Development Techdegree Graduate 12,204 PointsUsing Queue vs List
What are the advantages of using a Queue over looping through a List?
2 Answers
Boban Talevski
24,793 PointsIt really depends on what kind of list we are talking about.
We want to have a queue so we can have a FIFO structure so when we add a song, it gets added at the back of the queue, while whenever we want to play a song and pop it off, it gets removed from the head of the queue. This is basically what a FIFO data structure is.
The same thing can be accomplished with a list however, depending on the type. For example, we could use an ArrayList and simply add everything as the last element, and when removing, remove the first element. But the Queue interface is more appropriate as we know how add and remove should work for this structure, while in the ArrayList, we would have to add the specified index for each operation to make sure it behaves like a FIFO structure.
In both cases, java will probably do a similar thing (or maybe same even) behind the scenes, it's that the abstraction provided by the Queue interface fits our needs better and we don't have to worry how it works internally as long as add method adds the item at the back of the queue, and the pop method removes an item from the head of the queue.
Robert Angelo Valencia
Java Web Development Techdegree Graduate 12,204 PointsThanks for the thoughtful response, Boban!
Boban Talevski
24,793 PointsYou are welcome, glad it helped!