Here you can enter an element from front or from the rear end. I mean take a look at this diagram and see for yourself: Well it is a queue that allows us to push elements from both ends. It is the Deque (pronounce it as Deck because that’s how the cool kids do) interface. Of course, you can always go back to see what does what. So I am not going to describe them all over again. Other methods are pretty much the same that we had seen in ArrayList and LinkedList earlier. Like if you augment the following in the above code: Comparator c = q.comparator() If there’s no comparator provided and the ordering is innate, it will give you a result as null. You could choose to use a method called comparator() to provide your own ordering. Here’s the result you will get now: Īs you have noticed already that the ordering is kind of naturally done for you. The method clear() will simply remove everything from the list. Poll as we had learned earlier helps in extracting the head of the queue by removing it. Append the following code to the above code: ("Head of the queue: " + q.poll()) Let’s see what other methods we can put up in there to see what they do. If you run the program above, you will get: Īs you can see the elements are ordered as per their natural ordering. Notice here offer() method of Queue Interface does the same thing as add(). Here an example would be helpful: PriorityQueueq = new PriorityQueue(5) The methods of PriorityQueue class you can use are:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |