Java is a powerful programming language and it supports various data structures to make the life of programmers easy. In this article we will take a look at one such data structure that is Java Queue. These are the pointers this article focus on,
- Queue In Java
- Implementation Of Java Queue
- Methods In Java Queue
- Program To Demonstrate Queue Methods
- Iterating Through A Java Queue
Queue In Java
A queue is a data structure which follows the principle of FIFO (First-In-First-Out) i.e. the elements are inserted at the end of the list, and are deleted from the beginning of the list. This interface is available in the java.util.package and extends the Collection Interface.
Queue supports multiple methods, including insertion and deletion. The queues available in java.util.package are known as Unbounded Queues , while the queues present in the java.util.concurrent package are known are Bounded Queues.
All queues, except the Deques, support insertion at the end and deletion from the front. Deques support insertion and deletion of elements at both the ends.
Let us move to the next topic of this article on Java Queue,
Implementation Of Java Queue
In order to use the queue interface, we need to instantiate a concrete class. Following are the few implementations that can be used:
- util.LinkedList
- util.PriorityQueue
Since these implementations are not thread safe, PriorityBlockingQueue acts as an alternative for thread safe implementation.
Example:
Queue q1 = new LinkedList();
Queue q2 = new PriorityQueue();
Let us take a lok at some important Java Queue methods,
Methods In Java Queue
- add(): The add() method is used to insert elements at the end, or at the tail of the queue. The method is inherited from the Collection interface.
- offer(): The offer() method is preferable to the add() method, as it inserts the specified element into the queue without violating any capacity restrictions.
- peek(): The peek() method is used to look at the front of the queue without removing it. If the queue is empty, it returns a null value.
- element(): If the queue is empty, the method throws NoSuchElementException.
- remove(): The remove() method removes the front of the queue and returns it. Throws NoSuchElementException if the queue is empty.
- poll(): The poll() method removes the beginning of the queue and returns it. If the queue is empty, it returns a null value.
An overview of the following methods is given as follows:
Operation | Throws Exception | Returns Value |
Insert | add(element) | offer(element) |
Remove | remove() | poll() |
Examine | element() | peek() |
Let us take a look the demonstration now,
Program To Demonstrate Queue Methods
import java.util.*; public class Main { public static void main(String[] args) { //We cannot create instance of a Queue since it is an interface, thus we Queue<String> q1 = new LinkedList<String>(); //Adding elements to the Queue q1.add("I"); q1.add("Love"); q1.add("Rock"); q1.add("And"); q1.add("Roll"); System.out.println("Elements in Queue:"+q1); /* * We can remove an element from Queue using remove() method, *this removes the first element from the Queue */ System.out.println("Removed element: "+q1.remove()); /* *element() method - this returns the head of the *Queue. */ System.out.println("Head: "+q1.element()); /* *poll() method - this removes and returns the *head of the Queue. Returns null if the Queue is empty */ System.out.println("poll(): "+q1.poll()); /* *peek() method - it works same as element() method, *however, it returns null if the Queue is empty */ System.out.println("peek(): "+q1.peek()); //Displaying the elements of Queue System.out.println("Elements in Queue:"+q1); } }
Output:
Elements in Queue:[I, Love, Rock, And, Roll]
Removed element: I
Head: Love
poll(): Love
peek(): Rock
Elements in Queue:[Rock, And, Roll]. In the above example, Generic Queue has been used.
In this type of queue, we can limit the type of object inserted into the queue. In our example, we can have only string instances inserted into the queue.
Iterating Through A Java Queue
Elements in a java queue can be iterated using the following code:
Queue q1 = new LinkedList();
q1.add(“Rock”);
q1.add(“And”);
q1.add(“Roll”);
//access via Iterator
Iterator iterator = q1.iterator();
while(iterator.hasNext(){
String element = (String) iterator.next();
}
//access via new for-loop
for(Object object : q1) {
String element = (String) object;
}
The sequence in which the elements are iterated depends on the implementation of queue.
While there are multiple methods that a Java Queue can implement, the most important methods have been discussed here.
Thus we have come to an end of this article on ‘Java Queue’. If you wish to learn more, check out the Java Online Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.