Java Queue InterfaceJava 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 declarationMethods of Java Queue Interface
PriorityQueue classThe PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner. It inherits AbstractQueue class. PriorityQueue class declarationLet's see the declaration for java.util.PriorityQueue class. Java PriorityQueue ExampleOutput:head:Amit head:Amit iterating the queue elements: Amit Jai Karan Vijay Rahul after removing two elements: Karan Rahul Vijay Java PriorityQueue Example: BookLet'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. Output: 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 Next TopicJava Deque & ArrayDeque
|