0% found this document useful (0 votes)
24 views

Lecture 8 - Queue Implementation

The document discusses Java Queue implementations including LinkedList and PriorityQueue. It states that LinkedList is a standard queue implementation that stores elements in a linked list, making it fast to insert at the tail and remove from the head. It also describes how the Java Queue interface represents a data structure with elements inserted at the end and removed from the beginning, similarly to a supermarket queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 8 - Queue Implementation

The document discusses Java Queue implementations including LinkedList and PriorityQueue. It states that LinkedList is a standard queue implementation that stores elements in a linked list, making it fast to insert at the tail and remove from the head. It also describes how the Java Queue interface represents a data structure with elements inserted at the end and removed from the beginning, similarly to a supermarket queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA STRUCTURES AND ALGORITHMS

Queue implementation
Java Queue Implementations

• Since Queue is an interface you need to instantiate a concrete


implementation of the interface in order to use it. You can choose
between the following Queue implementations in the Java Collections API:

• java.util.LinkedList
• java.util.PriorityQueue
• LinkedList is a pretty standard queue implementation. Elements in the
queue are stored internally in a standard linked list data structure. This
makes it fast to insert elements at the end (tail) of the list, and remove
elements from the beginning (head) of the list.
Java Queue Implementations

The Java Queue interface, java.util.Queue represents a data structure designed to have elements inserted
at the end of the queue, and elements removed from the beginning of the queue. This is similar to how a
queue in a supermarket works. The Java Queue interface is a subtype of the Java Collection interface. It
represents an ordered sequence of objects just like a Java List, but its intended use is slightly different.
Because the Java Queue interface is a subtype of the Java Collection interface, all methods in the
Collection interface are also available in the Queue interface.
Java Queue Implementations
• Here are a few examples of how to create a Queue instance:

• Queue queueA = new LinkedList();


• Queue queueB = new PriorityQueue();
• In most Queue implementations the head and tail of the queue are
at opposite ends. It is possible, however, to implement the Queue
interface so that the head and tail of the queue is in the same end.
In that case you would have a stack.
Java Queue Implementations
• The Java Queue interface, java.util.Queue represents a data structure
designed to have elements inserted at the end of the queue, and elements
removed from the beginning of the queue. This is similar to how a queue in
a supermarket works.
Generic Queue
• By default you can put any Object into a Queue, but from Java 5, Java Generics makes it possible to limit
the types of object you can insert into a Queue. Here is an example:

• Queue<MyObject> queue = new LinkedList<MyObject>();


• This Queue can now only have MyObject instances inserted into it. You can then access and iterate its
elements without casting them. Here is how it looks:

• Queue<MyObject> queue = new LinkedList<MyObject>();

• MyObject myObject = queue.remove();

• for(MyObject anObject : queue){


• //do someting to anObject...
• }
Generic Queue

You might also like