Java Queue Interface
Java Queue interface orders the element in FIFO(First In First Out) manner. In FIFO, first element is removed first and last element is removed at last.
Queue Interface declaration
Methods of Java Queue Interface
The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner. It inherits AbstractQueue class.
PriorityQueue class declaration
Let's see the declaration for java.util.PriorityQueue class.
Java PriorityQueue ExampleTest it Now
Output:head:Amit head:Amit iterating the queue elements: Amit Jai Karan Vijay Rahul after removing two elements: Karan Rahul Vijay
Java PriorityQueue Example: Book
Let's see a PriorityQueue example where we are adding books to queue and printing all the books. The elements in PriorityQueue must be of Comparable type. String and Wrapper classes are Comparable by default. To add user-defined objects in PriorityQueue, you need to implement Comparable interface.
Traversing the queue elements: 101 Data Communications & Networking Forouzan Mc Graw Hill 4 233 Operating System Galvin Wiley 6 121 Let us C Yashwant Kanetkar BPB 8 After removing one book record: 121 Let us C Yashwant Kanetkar BPB 8 233 Operating System Galvin Wiley 6