Java – Queue interface with method details

In this article, we will discuss Queue interface with all its important methods in detail

1. Key points about Queue:

  • Queue interface follows First-In First-Out
  • i.e.; FIFO to order elements

2. Queue interface:

  • Queue is a sub-interface of Collection interface (i.e.; Queue extends Collection)
  • To represent a group of element/objects as a single unit/entity, which follows FIFO order then programmer should consider using Queue
  • Example: before sending emails to all recipients, their corresponding email Id need to be stored and finally triggered to send out email one-by-one
  • Used to store group of element/objects before processing for any activity as mentioned above for email activity
  • There are various concrete classes for Queue interface viz.; PriorityQueue, LinkedBlockingQueue, PriorityBlockingQueue,
  • Queue interface defines various Queue specific methods; in addition to methods inherited from Collection interface
  • These methods can be used to operate on Queue objects only
  • Using Queue specific methods, we can poll, peek or remove an object
  • Note: From Java 1.5 version, LinkedList also implements Queue/Deque interface in addition to List interface (check figure for understanding)
  • Present in java.util package and extends java.util.Collection interface
40-Queue-interace-in-java

Source: Team BenchResources.Net

3. Queue interface methods:

 Queue methodsDescription
boolean offer(Object obj);to add an specified object into Queue
boolean add(Object obj);similar to offer() method but throws IllegalStateException for space constraints
Object peek();retrieve head element without removing from Queue

 

returns null, if Queue is empty

Object element();retrieves, head of the Queue but doesn’t remove
Object poll();retrieve & remove head element from Queue
returns null, if Queue is empty
Object remove();retrieve & remove head element from Queue

Note: Queue methods listed above will be inherited to subsequent Queue implemented classes

4. Below listed classes implements Queue interface

  • PriorityQueue
  • PriorityBlockingQueue (through BlockingQueue interface)
  • LinkedBlockingQueue (through BlockingQueue interface)

5. Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - PriorityQueue class with example
Java - Properties class with example