Collections - Queue

Collections - Queue
The java.util.Queue is a subtype of java.util.Collection interface. It is an ordered list of objects with its use limited to inserting elements at the end of list and deleting elements from the start of list i.e. it follows FIFO principle.
Since it is an interface, we need a concrete class during its declaration. There are many ways to initialize a Queue object, most common being-
  1. As a Priority Queue
  2. As a LinkedList
Please note that both the implementations are not thread safe. PriorityBlockingQueue is one alternative implementation if you need a thread safe implementation.
Operations on Queue :
  • Add()-Adds an element at the tail of queue. More specifically, at the last of linkedlist if it is used, or according to the priority in case of priority queue implementation.
  • peek()-To view the head of queue without removing it. Returns null if queue is empty.
  • element()-Similar to peek(). Throws NoSuchElementException if queue is empty.
  • remove()-Removes and returns the head of the queue. Throws NoSuchElementException when queue is impty.
  • poll()-Removes and returns the head of the queue. Returns null if queue is empty.
These are the main features of using the arraylist for certain operations in java.For more tutorial and supports for you,please visit cheap website designers in bangalore.

Comments