Sunday 6 April 2014

Queue Interface

In addition to Collection Operations, Queue interface provides additional operations like returning the Head of the Queue, Retrieving and removing the Head Element of the Queue etc.,


public interface Queue<E> extends Collection<E> {
  boolean offer(E e);
  E remove();
  E poll();
  E element();
  E peek();
}
 

There are many implementations for the Queue interface, LinkedList and PriorityQueue are most useful ones.



Example
   Queue queue1 = new LinkedList();
   Queue queue2 = new PriorityQueue();

Each Queue method exists in two forms:
   1. one throws an exception if the operation fails, and 
   2. the other returns a special value if the operation fails  
  
Operation Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment