In this chapter you will learn:
- What is queue interface?
- Queue Structure
- Programming Example
What is Queue Interface?
Queue is a part of java collection that follows FIFO structure for holding elements. FIFO is based on First In First Out that means the first inserted element will be processed first. There are various real life examples of Queues are:
- Ticket Counter Queue – The First person in the line gets chance to buy ticket first.
- Bus Queue – First position bus will start first.
- Billing Counter – First customer in the line will pay the bill first.
More about Queue
- Queue has additional insertion, removal and inspection operations besides on basic collection operations.
- Each Queue operation gets terminated by two methods either by throwing exception or by returning special value as null or false.
- Each queue has two parts head and tail. Items inserted in queue through tail side and remove item through head side.
- Bounded queue holds only specified number of elements.
Add()
method is used for inserting element in a queue andremove()
orpoll()
method is used for removing items from queue.- Queue doesn’t allow the insertion of null.
Methods of Queue Interface
Methods | Descriptions |
boolean add (E e) |
This method inserts an element in a queue and returns true if operation successful or return IllegalStateException exception. |
E element( ) |
This method returns element from the top of the queue. It only retrieves element and returns to you. It doesn’t remove element. |
boolean offer(E e) |
This method is also used for adding element in a queue. It is useful when queue is bounded and accept only limit number of element. It returns true if element is added successfully and return false if operation fails. The add(E e) method throws exception when operation gets unsuccessful. |
E peek( ) |
This methods returns element from the top of the queue. The difference between E element() and E peek() method is E peek() returns null if element not found whereas E element throws NoSuchElementException exception. |
E poll() |
This methods removes item from the top of the queue. It returns null if operation gets failed. |
E remove() |
It also removes item from the top of the queue but returnsNoSuchElementException if queue is empty. |
Declaration
Queuequeue = new LinkedList ();
Programming Example
import java.util.*; class Queue_Example { public static void main(String[] args) { Queue q=new LinkedList(); //Adding Item in a queue q.add("Java"); q.add("C#"); q.add("VB.Net"); q.add("C++"); q.add("PHP"); //Size of Queue System.out.println("Size of queue : "+q.size()); //Displaying Queue Iterator itr=q.iterator(); while(itr.hasNext()) { System.out.print((String)itr.next()+ " "); } //Retrieving Queue Element System.out.println("\nTop Item is "+q.peek()); //Remove Top Element System.out.println("Items Removed "+q.remove()); //Again printing queue System.out.println(q); //Size of Queue System.out.println("Size of queue : "+q.size()); //Adding an Item q.add("HTML"); //Printing Queue System.out.println(q); } }
Output
Size of queue : 5
Java C# VB.Net C++ PHP
Top Item is Java
Items Removed Java
[C#, VB.Net, C++, PHP]
Size of queue : 4
[C#, VB.Net, C++, PHP, HTML]
_
Java C# VB.Net C++ PHP
Top Item is Java
Items Removed Java
[C#, VB.Net, C++, PHP]
Size of queue : 4
[C#, VB.Net, C++, PHP, HTML]
_
Summary
In this chapter you learned what the queue interface is in java and their methods and implementation with programming example. In the next chapter you will learn PriorityQueue class.