Let's take a look at how this works with a simple unit test: PriorityQueue integerQueue = new PriorityQueue() ĭespite the order in which our integers were added to the Priority Queue, we can see that the retrieval order is changed according to the natural order of the numbers. When new elements are inserted into the Priority Queue, they are ordered based on their natural ordering, or by a defined Comparator provided when we construct the Priority Queue. One such exception to this rule is the PriorityQueue. PlanetsQueue.We saw earlier that most of the Queues that we come across in Java follow the FIFO principle. This is because by that time, the Queue is empty and peekFirst has nothing to retrieve, it returns null. In the following example, we used peekFirst function, to get first planet from the Queue. This is alternative to the getFirst function / first property, which throws Exception when the retrieval of element from the Queue failed. PeekFirst or peek function tries to get the first element from the queue, and if it fails, returns null instead of exception. ![]() Null Safely get element from Queue - peekFirst This is because by that time, the Queue is empty and pollFirst has nothing to remove, it returns null. In the following example, we used pollFirst function, to remove planets from the Queue. This is alternative to the removeFirst function, which throws Exception when the removal of element from the Queue failed. PollFirst function Tries to remove the element from the queue, and if it fails, returns null instead of exception. Jupiter Safely remove element from Queue - pollFirst Println("Offer last result = $offerLast") Val offerLast: Boolean = planetsQueue.offerLast("Jupiter") ![]() We are also printing the result of offerLast function. In the following example, we used offerLast function to add Jupiter to end of our Queue. This is alternative to addLast, which will throw exception if the Queue capacity constraints are reached. It returns false if adding element to the end of queue failed. Tries to add element to end of queue while checking for capacity constraints. Queue is empty! Returning Safely add element to Queue - offerLast In the following example, we used isEmpty to check if the Queue is empty, before we tried to access the first element in the Queue. We can use the method, isEmpty() to check if the Queue is empty or not, before attempting to access the first element. catch block we saw in the Exception Handling section of the tutorial. To avoid the and crashing our program, we can use a try. In the following example, we removed all the elements from the Queue and then tried to access the First element in the Queue.Įxception in thread "main" Īt java.base/(LinkedList.java:248)Īt .main(CosmicTestDriver.kt:29) Check if Queue is empty What happens if queue is empty, and we attempt to access the first element? In that case, we will encounter. In the following example, we are accessing first property to find out the first element in the Queue. Instead of removing an element, if we want to just see the first element in the Queue, for such a usecase, we can use first property.įirst property on the Queue returns us the first element in the Queue. Neptune Retrieve the first element from Queue We can check if Queue is empty or not using the isEmpty method. ![]() ![]() In the following example, we used removeFirst method to remove and get the elements from the Queue until the Queue is empty. We can remove and get the First element from the Queue using removeFirst method or remove method. In the following example, we used addLast method, to add Jupiter to the end of our Queue. We can add element to the end of a Queue, using addLast method or add method. PlanetsQueue.addAll(listOf("Mercury", "Venus", "Earth")) We are creating a LinkedList with three planets to begin with. Creating a QueueĬonsider a Queue of planets as shown below. In this tutorial, we will see how to use LinkedList to perform operations on a Queue. Queue interface is extended by following sub interfaces. Queue implementation is provided by following classes. The most popular implementation of this interface is provided by LinkedList. The person who came in last, will be the last to get the ticket. The person who came in first, will be the first to get the ticket. Imagine going to a movie theater and purchasing a ticket. As a concept, Queue provides, a First in - First Out data structure.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |