ologyfert.blogg.se

Java queue
Java queue











  • Key Points to Remember Regarding the Queue Interface.
  • The person who comes in the last is standing in the last of the line and the person who gets the ticket is removed from the beginning of the Queue. The Queue interface basically orders the elements in FIFO(first-in, first-out) manner.Ī railway reservation counter is our daily life example of a Queue, where the people stand outside the reservation counter. A Queue is an ordered list in which element insertions are done at one end(rear) and deletions are done at the other end(front). package com.Queue java example: Queue in Java is an interface that is present in java.util package. Its sorting logic is that the person who has longest name will be the first element in the queue and the person with smallest name would be the last person in the list. In the below example, we will create a Person class that implements Comparable interface. We can either use Comparable interface or Comparator interface. Now, we will see how we can define our own priority of elements. We can see in the results that Sam is coming before Rameez in iterator method, which is not alphabetical order. (" Name using poll method : " + nameQueue.poll()) Let’s understand this using below example package com.javatrainingschool

    java queue

    Therefore, we have to use poll method to test the priority order of elements. Because if you simply iterate over a priority queue using an iterator or for-each loop, you may not get the priority order in which the queue will out the elements from it.

    #Java queue how to

    How to retrieve the elements in the order of their priority from priority queue The only difference is that poll method return null, whereas remove() throws exception if the queue is empty. Object poll() //retrieves and removes the head element Object remove() //retrieves and removes the head element There are two metods to fetch and remove head element of a queue. Whereas element() throws exception if the queue is empty Fetch and remove the head element of the queue The only difference between the two methods is that peek() returns null if the queue is empty. Object peek() //retrieves the head element of the queue

    java queue

    Object element() //retrieves the head element of the queue There are two methods using which we can retrieve head element of a queue. Head element of a queue is the first element of the queue that is out first from the queue. Poll method retrieves the head element of the queue and removes it. One more thing to notice here is that we have used poll() method inside while loop. In the output, we can see that elements have been displayed in the alphabetical order which is natural sorting order of String objects. Let’s see one example using default priority of String objects which is alphabetical. If we want to override the default priority, then we have to use comparision methods, such as Comparable or Comparator. How is priority decided?ĭefaultpPriority of elements is natural sorting order as per their data type. Therefore, he/she would stand in the queue before all others.

    java queue

    If a critical patient comes in, his/her priority to be seen by the doctor is higher than others. The higher priority element is placed before a lower priority element.Ī practical example of a priority queue would be a queue in a hospital. This orders its elements based on the priorities. The implementing class of Queue interface is PriorityQueue. Public interface Queue extends Collection PriorityQueue Class It means the element that goes first in a queue, comes out first and one that goes last, comes last. Queue is a data structure that maintains FIFO (First In First Out) order of elements.











    Java queue