All for Joomla All for Webmasters

Java Queue Interface

The Queue interface basically orders the element in FIFO(First In First Out)manner.

Methods of Queue Interface :

  1. public boolean add(object);
  2. public boolean offer(object);
  3. public remove();
  4. public poll();
  5. public element();
  6. public peek();

PriorityQueue class:

The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner.

Example of PriorityQueue:

  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
PinIt
submit to reddit

Leave a Reply

Top