Sunday, September 15, 2019

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

  1. public interface Queue<E> extends Collection<E>  

Methods of Java Queue Interface

MethodDescription
boolean add(object)It is used to insert the specified element into this queue and return true upon success.
boolean offer(object)It is used to insert the specified element into this queue.
Object remove()It is used to retrieves and removes the head of this queue.
Object poll()It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
Object element()It is used to retrieves, but does not remove, the head of this queue.
Object peek()It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

PriorityQueue class

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.

  1. public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable  

Java PriorityQueue Example

  1. import java.util.*;  
  2. class TestCollection12{  
  3. public static void main(String args[]){  
  4. PriorityQueue<String> queue=new PriorityQueue<String>();  
  5. queue.add("Amit");  
  6. queue.add("Vijay");  
  7. queue.add("Karan");  
  8. queue.add("Jai");  
  9. queue.add("Rahul");  
  10. System.out.println("head:"+queue.element());  
  11. System.out.println("head:"+queue.peek());  
  12. System.out.println("iterating the queue elements:");  
  13. Iterator itr=queue.iterator();  
  14. while(itr.hasNext()){  
  15. System.out.println(itr.next());  
  16. }  
  17. queue.remove();  
  18. queue.poll();  
  19. System.out.println("after removing two elements:");  
  20. Iterator<String> itr2=queue.iterator();  
  21. while(itr2.hasNext()){  
  22. System.out.println(itr2.next());  
  23. }  
  24. }  
  25. }  

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.

  1. import java.util.*;  
  2. class Book implements Comparable<Book>{  
  3. int id;  
  4. String name,author,publisher;  
  5. int quantity;  
  6. public Book(int id, String name, String author, String publisher, int quantity) {  
  7.     this.id = id;  
  8.     this.name = name;  
  9.     this.author = author;  
  10.     this.publisher = publisher;  
  11.     this.quantity = quantity;  
  12. }  
  13. public int compareTo(Book b) {  
  14.     if(id>b.id){  
  15.         return 1;  
  16.     }else if(id<b.id){  
  17.         return -1;  
  18.     }else{  
  19.     return 0;  
  20.     }  
  21. }  
  22. }  
  23. public class LinkedListExample {  
  24. public static void main(String[] args) {  
  25.     Queue<Book> queue=new PriorityQueue<Book>();  
  26.     //Creating Books  
  27.     Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8);  
  28.     Book b2=new Book(233,"Operating System","Galvin","Wiley",6);  
  29.     Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  
  30.     //Adding Books to the queue  
  31.     queue.add(b1);  
  32.     queue.add(b2);  
  33.     queue.add(b3);  
  34.     System.out.println("Traversing the queue elements:");  
  35.     //Traversing queue elements  
  36.     for(Book b:queue){  
  37.     System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  
  38.     }  
  39.     queue.remove();  
  40.     System.out.println("After removing one book record:");  
  41.     for(Book b:queue){  
  42.         System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  
  43.         }  
  44. }  
  45. }  
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

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...