Java using Queue and Deque example

May 02, 2020 | No comments

QUEUE IN JAVA

Queue is FIFO(First in First Out) data structure, which means that element inserted first will be removed first. In Java Queue is an interface and it provides multiple implementation of Queue with different properties, which makes certain implementation better suited for certain scenarios. Queue interface extends the Collection Interface so it provides common collection methods like size(), isEmpty(), addAll(), clear() etc. This article covers some of the common examples of using Queue in java
Most generally when we need a FIFO data structure we can use PriorityQueue, LinkedList, ArrayDeque classes. They can be classified as Queue and Deque.

Queue

The interface java.util.Queue is the Queue interface. PriorityQueue, LinkedList are the most common Queue implementations. When we need Queue we can use instance of any of the PriorityQueue, LinkedList classes.
Queue Implementations
  • PriorityQueue
  • LinkedList
PriorityQueue Example
Create instance of PriorityQueue, add 5 items to it. Print the size, and peek the first item.
Queue q1 = new PriorityQueue();

//Add 5 items to q1
q1.offer("a");
q1.offer("b");
q1.offer("c");
q1.offer("d");
q1.offer("e");

System.out.println("Size of q1 = " + q1.size());
System.out.println("First Element of q1 = " + q1.peek());
Queue Iteration
Iterate over a Queue and print its item.
//iterate
System.out.println("Iterating on Queue");
Iterator it1 = q1.iterator();

while (it1.hasNext()) {
  String item = it1.next();
  System.out.print(item + " ");
}

Deque

java.util.Deque is the Deque interface. Deque is also known as double ended Queue. That means we can add and remove items at both ends of the queue. LinkedList and ArrayDeque implements DeQueue Interface(Double ended queue).
Deque Implementations
  • LinkedList
  • ArrayDeque
Deque Example
Create two queue q2 and q3. Since we are creating instances of Deque, we can access both end of the contents. Like peekFirst(), peekLast() to peek the first and last element respectively.
Deque q2 = new LinkedList();
Deque q3 = new ArrayDeque();
System.out.println("First Element of q2 = " + q2.peekFirst());
System.out.println("Last  Element of q2 = " + q2.peekLast());

Concurrent Queue Implementations

The java.util.concurrent package contains BlockingQueue and its implemented classes. BlockingQueue is used in multi threaded application, when we want a thread to wait or blocked unless we have item in the thread.
BlockingQueue implemented classes.
  • LinkedBlockingQueue — an optionally bounded FIFO blocking queue backed by linked nodes
  • ArrayBlockingQueue — a bounded FIFO blocking queue backed by an array
  • PriorityBlockingQueue — an unbounded blocking priority queue backed by a heap
  • DelayQueue — a time-based scheduling queue backed by a heap
  • SynchronousQueue — a simple rendezvous mechanism that uses the BlockingQueue interface
Summary
  • PriorityQueue, LinkedList, ArrayDeque are suitable in most cases.
  • Deque can be used as a stack also.
  • LinkedList and ArrayDeque are most commonly Deque implementations.
  • PriorityQueue is most commonly used Queue implementation

No comments :

Post a Comment

Please leave your message queries or suggetions.