Queue in Java with Example - Javastudypoint

Monday, March 11, 2019

Queue in Java with Example

Queue in Java is an interface which is present in java.util package. It extends the collection interface. A Queue is an ordered list in which elements insertions are done at one end(rear) and deletions are done at the other end(front). The Queue interface basically orders the elements in FIFO(first in, first out) manner. A railway reservation counter is our daily life example of Queue, where the people stands outside the reservation counter. The person who comes in the last is standing in the last of the line and the person who gets the ticket is removed from the beginning of the Queue.
Queue in Java




Points to remember about Queue Interface.

1. Child interface of collection interface.
2. Null values are not allowed.
3. Duplicates values are not allowed.
4. Heterogeneous elements are allowed.
5. Based on the FIFO manner. 
6. LinkedList and PriorityQueue are two implementation class of Queue interface.
7. We cannot instantiate it because it is an interface, rather than we create the instance of LinkedList or PriorityQueue.

Methods of Java Queue interface:


1. boolean add(Object): This method is used to insert the specified element at the end of the queue. It returns true if the element is successfully inserted. It throws an exception if no space is currently available in the queue.

2. Object remove(): This method is used to remove the element at the head of the Queue and returns its value. It throws NoSuchElementException if the queue is empty.

3. Object peek(): This method returns the element at the head of the queue without removing the element. It returns null if the queue is empty.

4. Object poll(): This method is similar to remove() method but the only difference is it returns null if the Queue is empty.

5. Object element(): This method returns the element at the head of the queue, the element is not removed. It throws NoSuchElementException if the queue is empty.


6. boolean offer(Object obj): This method is used to insert the specified element into this queue. It returns true if the element is successfully inserted otherwise it returns false.


Java Queue Example:


import java.util.*;
class Examplequeue{

 public static void main(String args[]){
  Queue<String> queue = new LinkedList<String>();

  //adding element into this queue
  queue.add("Amit");
  queue.add("Raaj");
  queue.add("Ajay");
  queue.add("Vijay");
  queue.add("Rahul");

  //Queue methods operations
  System.out.println("The Queue elements are: " +queue);
  System.out.println("The removal element is: " +queue.remove());
  System.out.println("After remove Queue elements are: " +queue);
  System.out.println("Queue head element is: " +queue.peek());
  System.out.println("Return Queue elements: " +queue.poll());
  System.out.println("After remove Queue elements are: " +queue);
  System.out.println("Return Queue elements without removing: " +queue.element());
  System.out.println("After all operations Queue elements are: " +queue);
 }
}

Output:
Queue in Java with Example



Java Queue Example peek() vs element():



import java.util.*;
class Examplequeue{
 public static void main(String args[]){
 Queue<String> q = new LinkedList<String>();

 //Queue methods operation
 System.out.println("Head Queue element: " +q.peek()); //return null
 System.out.println("Return Queue element: " +q.element());  //throws exception
 }

}

Output:
Queue in Java with Example


Java Queue Example remove() vs poll():



import java.util.*;
class Examplequeue{
 public static void main(String args[]){
 Queue<String> q = new LinkedList<String>();

 //Queue methods operation
 System.out.println("The removal Queue element: " +q.remove()); //throws exception
 System.out.println("Return Queue element: " +q.poll());  //return null
 }

}

Output: In case of remove() method 

Queue in Java with Example

Output: In case of poll() method
Queue in Java with Example



You may also like these related posts:
How to remove a specified element in the ArrayList?
How to implement Vector class in Java?
How to implement a Stack data structure in Java?
HashSet class in Java with the example

LinkedHashSet class in Java with an example
SortedSet interface in Java with the example
NavigableSet interface in Java with an example
How to implement a tree data structure in Java?

No comments:

Post a Comment