


What if we want to serve the request based on the priority rather than FIFO? This can be done with the help of PriorityQueue, which serves the request based on the priority, that we set using Comparator. We have seen how a queue serves the requests on the basis of FIFO(First in FIrst out). The only difference between peek() and element() is that peek() method returns null if the Queue is empty. The only difference between poll() and remove() is that poll() method returns null if the Queue is empty.Į peek(): This method is almost same as element() method. Returns true if the the element is added successfully or false if the element is not added that basically happens when the Queue is at its max capacity and cannot take any more elements.Į element(): This method returns the head (the first element) of the Queue.īoolean offer(object): This is same as add() method.Į remove(): This method removes the head(first element) of the Queue and returns its value.Į poll(): This method is almost same as remove() method. Methods of Queue interfaceīoolean add(E e): This method adds the specified element at the end of Queue. If you try to add the element of the type not specified then you will get compilation error, this brings the safety in our program and makes it less error prone. As you can see I have specified the type of the Queue as String using Generics, so that it accepts only String elements. In the above example, I have used Generics, this helps us to specify the type of the element that we are going to insert into the collection. * however it returns null if the Queue is empty * peek() method - it works same as element() method, This program will schedule tasks to our work queue, so lets name it NewTask.java: String message String.join(, argv) channel.basicPublish(, hello. * poll() method - this removes and returns the * element() method - this returns the head of the * this would remove the first element from the Queue * We can remove element from Queue using remove() method, * interface, we can create instance of LinkedList or * We cannot create instance of a Queue as it is an Queue q2 = new PriorityQueue() Java Queue Example import java.util.* Queue is an interface so we cannot instantiate it, rather we create instance of LinkedList or PriorityQueue and assign it to the Queue like this: Queue q1 = new LinkedList() The Queue interface of the Java collections framework provides the functionality of the queue data structure. Queue interface in Java collections has two implementation: LinkedList and PriorityQueue, these two classes implements Queue interface.

The concept here is similar to the queue we see in our daily life, for example, when a new iPhone launches we stand in a queue outside the apple store, whoever is added to the queue has to stand at the end of it and persons are served on the basis of FIFO (First In First Out), The one who gets the iPhone is removed from the beginning of the queue. A Queue is designed in such a way so that the elements added to it are placed at the end of Queue and removed from the beginning of Queue.
