Queue interface

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

Queue interface follows First-In First-Out (i.e.; FIFO), to order the elements

 

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

 

 

Queue interface methods:

 Queue methods Description
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

 

 

Below listed classes implements Queue interface

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

 

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

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html
https://docs.oracle.com/javase/8/docs/api/java/util/List.html
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html

 

Happy Coding !!
Happy Learning !!