10/11/2023 0 Comments Priority queue java example![]() Your email address will not be published. ![]() boolean add(element) : This method will insert the element into this priority queue.PriorityQueue(int initialCapacity, Comparator comparator): It is similar to PriorityQueue(int initialCapacity), but the extra parameter specifies the order of its element, which should be according to the specified comparator.PriorityQueue(int initialCapacity): It is similar to the PriorityQueue() with the specified initial capacity.PriorityQueue(Collection c): It creates Priority Queue containing elements in the collection specified in the argument.PriorityQueue(): Creating a Priority Queue with default initial capacity and following the natural ordering for its elements.The basic retrieval operations like, poll(), remove(), peek() access the element at the front or at the head of the queue.Null is not allowed, as we can’t compare it with any value.Non-comparable objects cannot be implemented using Priority Queue.Since the element is removed from the front, therefore the least element is removed first. In the given example, the front of the priority queue contains the smallest element, as per the specified ordering, and the rear contains the greatest element. You would like to read thinner books first and gradually move to thicker books. Let’s say you want to read few books and prioritize it based on number of pages. Now, even here we can modify our system, that senior citizens are serviced first, so this is kind of a system where we have set few priorities (maybe on the basis of age). Suppose two-person went to two different banks to deposit some money, Person A is serviced on the basis of the queue formed on the cash counter, Person B is serviced according to the token numbers provided by the bank (Underlying Queue), now here both follows FIFO (First in First Out) model, but their priorities are determined by different factors, for Person A it’s their arrival time (standing in a queue), but for Person B it’s just the token number (smaller number serviced first).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |