Queue Interface

A Queue is a collection designed for processing elements generally in the First In First Out (FIFO) order.

 

Important properties of Queue interface

  1. Methods in Queue interface generally exists in two forms:

    • one throws an exception if the operation fails,

    • the other returns a special value (either null or false, depending on the operation).

      • this type is designed mainly for use with capacity-restricted Queue implementations;

        • insert operations cannot fail in most implementations.

  2. Queues generally order elements in a FIFO (first-in-first-out) manner.

    • Exceptions are:

      • Priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and

      • LIFO queues (or stacks) which order the elements LIFO (last-in-first-out).

  3. The head of the queue is the element which would be removed by a call to remove() or poll().

  4. In a FIFO queue, all new elements are inserted at the tail of the queue.

    • Other kinds of queues may use different placement rules. 

  5. The offer method inserts an element if possible, otherwise returning false.

    • This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception.

    • The offer method is designed for use when failure is a normal, like, in fixed-capacity (or "bounded") queues.

  6. The remove() and poll() methods remove and return the head of the queue.

    • The remove() and poll() methods differ only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null.

  7. The element() and peek() methods return, but do not remove, the head of the queue.

  8. The Queue interface does not define the blocking queue methods, which are common in concurrent programming. 

  9. Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null.

    • Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.

  10. Queue implementations generally inherit the identity based versions of equals and hashCode from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties.

 

Important methods of Queue interface

  • add(E e)

    • Inserts the specified element into this queue, returning true upon success and throwing an IllegalStateException if no space is currently available.

  • element()

    • Retrieves, but does not remove, the head of this queue.

  • offer(E e)

    • Inserts the specified element into this queue, returning true if insert was successful.

  • peek()

    • Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

  • poll()

    • Retrieves and removes the head of this queue, or returns null if this queue is empty.

  • remove()

    • Retrieves and removes the head of this queue.

 

Important notes

  1. BlockingQueue interface that extends from the Queue interface belongs to java concurrency package and will be discussed when we discuss about java concurrency.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)