The Quque Interface


SUBMITTED BY: Guest

DATE: Dec. 5, 2013, 5:22 p.m.

FORMAT: Text only

SIZE: 4.9 kB

HITS: 928

  1. The Queue Interface
  2. A Queue is a collection for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, removal, and inspection operations. The Queue interface follows.
  3. public interface Queue<E> extends Collection<E> {
  4. E element();
  5. boolean offer(E e);
  6. E peek();
  7. E poll();
  8. E remove();
  9. }
  10. 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 (either null or false, depending on the operation). The regular structure of the interface is illustrated in the following table.
  11. Queue Interface Structure
  12. Type of Operation Throws exception Returns special value
  13. Insert add(e) offer(e)
  14. Remove remove() poll()
  15. Examine element() peek()
  16. Queues typically, but not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to their values — see the Object Ordering section for details). Whatever ordering is used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.
  17. It is possible for a Queue implementation to restrict the number of elements that it holds; such queues are known as bounded. Some Queue implementations in java.util.concurrent are bounded, but the implementations in java.util are not.
  18. The add method, which Queue inherits from Collection, inserts an element unless it would violate the queue's capacity restrictions, in which case it throws IllegalStateException. The offer method, which is intended solely for use on bounded queues, differs from add only in that it indicates failure to insert an element by returning false.
  19. The remove and poll methods both remove and return the head of the queue. Exactly which element gets removed is a function of the queue's ordering policy. The remove and poll methods differ in their behavior only when the queue is empty. Under these circumstances, remove throws NoSuchElementException, while poll returns null.
  20. The element and peek methods return, but do not remove, the head of the queue. They differ from one another in precisely the same fashion as remove and poll: If the queue is empty, element throws NoSuchElementException, while peek returns null.
  21. Queue implementations generally do not allow insertion of null elements. The LinkedList implementation, which was retrofitted to implement Queue, is an exception. For historical reasons, it permits null elements, but you should refrain from taking advantage of this, because null is used as a special return value by the poll and peek methods.
  22. Queue implementations generally do not define element-based versions of the equals and hashCode methods but instead inherit the identity-based versions from Object.
  23. The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the interface java.util.concurrent.BlockingQueue, which extends Queue.
  24. In the following example program, a queue is used to implement a countdown timer. The queue is preloaded with all the integer values from a number specified on the command line to zero, in descending order. Then, the values are removed from the queue and printed at one-second intervals. The program is artificial in that it would be more natural to do the same thing without using a queue, but it illustrates the use of a queue to store elements prior to subsequent processing.
  25. import java.util.*;
  26. public class Countdown {
  27. public static void main(String[] args) throws InterruptedException {
  28. int time = Integer.parseInt(args[0]);
  29. Queue<Integer> queue = new LinkedList<Integer>();
  30. for (int i = time; i >= 0; i--)
  31. queue.add(i);
  32. while (!queue.isEmpty()) {
  33. System.out.println(queue.remove());
  34. Thread.sleep(1000);
  35. }
  36. }
  37. }
  38. In the following example, a priority queue is used to sort a collection of elements. Again this program is artificial in that there is no reason to use it in favor of the sort method provided in Collections, but it illustrates the behavior of priority queues.
  39. static <E> List<E> heapSort(Collection<E> c) {
  40. Queue<E> queue = new PriorityQueue<E>(c);
  41. List<E> result = new ArrayList<E>();
  42. while (!queue.isEmpty())
  43. result.add(queue.remove());
  44. return result;
  45. }

comments powered by Disqus