Java Reference
In-Depth Information
int initialCapacity = 5;
Comparator<ComparablePerson> nameComparator =
Comparator.comparing(ComparablePerson::getName);
Queue<ComparablePerson> pq =
new PriorityQueue<>(initialCapacity, nameComparator);
pq.add(new ComparablePerson(1, "John"));
pq.add(new ComparablePerson(4, "Ken"));
pq.add(new ComparablePerson(2, "Richard"));
pq.add(new ComparablePerson(3, "Donna"));
pq.add(new ComparablePerson(4, "Adam"));
System.out.println("Priority queue: " + pq);
while (pq.peek() != null) {
System.out.println("Head Element: " + pq.peek());
pq.remove();
System.out.println("Removed one element from Queue");
System.out.println("Priority queue: " + pq);
}
}
}
Priority queue: [(4, Adam), (3, Donna), (2, Richard), (4, Ken), (1, John)]
Head Element: (4, Adam)
Removed one element from Queue
Priority queue: [(3, Donna), (1, John), (2, Richard), (4, Ken)]
Head Element: (3, Donna)
Removed one element from Queue
Priority queue: [(1, John), (4, Ken), (2, Richard)]
Head Element: (1, John)
Removed one element from Queue
Priority queue: [(4, Ken), (2, Richard)]
Head Element: (4, Ken)
Removed one element from Queue
Priority queue: [(2, Richard)]
Head Element: (2, Richard)
Removed one element from Queue
Priority queue: []
Double Ended Queues
A doubly ended queue or deque is an extended version of a queue to allow insertion and removal of elements from
both ends (the head and the tail). An instance of Deque represents a doubly ended queue. The name Deque does not
mean opposite of Queue . Rather, it means “ D ouble e nded que ue”. It is pronounced “deck,” not “de queue.”
The Deque interface extends the Queue interface. It declares additional methods to facilitate all the operations for a
queue at the head as well as at the tail. It can be used as a FIFO queue or a LIFO queue. You already know what a Queue
is and how to use it. A Deque is just another version of a queue that can be used to represent different kinds of queues,
not just a FIFO queue. All you have to do in this section is learn about the new methods that the Deque interface offers.
 
Search WWH ::




Custom Search