![]() The Queue is a special interface in Java that is used to hold the elements in insertion order. This is similar to the previous example’s LinkedList operation, but note the head element is 1 because it’s a PriorityQueue. Example #5 – Examine operation on PriorityQueue We have printed the queue after the peek operation, and it can be seen that the head element, i.e. Note here that we have used the peek() function, which will return the head of the queue without actually removing it. Example #4 – Examine operation on LinkedList The removal is based totally on the value of elements. Note the insertion order and removal order is not the same. Here, we have used PriorityQueue, which will hold and return the elements depending upon the natural ordering of elements or upon the comparator, if any passed. Example #3 – Insertion and Removal Operation on PriorityQueue We have printed the queue after the removal of all the elements that’s why we are getting an empty bracket in the end. The removal order is the same as per the insertion. ![]() Here, we have used function isEmpty() to check when the queue becomes empty after the removal of elements. Example #2 – Let’s remove the added elements one by one Note here that the order of insertion is the same with output from left to write. Given below are the different examples of Queue in Java: Example #1 – Add operation with LinkedList implementation In the case of remove operation, an object will be removed actually from the queue but in the case of examine the object will be returned without actually removing it from the queue. There are three types of operation in this kind of operation: an insertion, the second is removal, and the third is retrieval or examine. So as explained, there are two types of methods that throw an exception and which return special value.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |