Queue is a Data Structure. it stores the data in ordered from and delete data in First In First Out.
In java, Queue is an Interface to implement this functionality. Queue interface is inside the java.util package. It extends Collection Interface.
If we need to use this data structure in our program, then we need class like LinkedList or PriorityQueue to declare Queue.
Queue interface has certain methods to achieve inserting, removing functionality.
List of Methods
To add element in the Queue
- add(element) -> It simply add element to the queue. It returns IllegalStateException if Queue size can not be increased.
- offer(element) -> It is same as add() method but it does not return Exception.
To retrieve element from the Queue
- peek() -> It retrieve first(HEAD) element from the Queue but it does not remove element from the Queue.It returns null if Queue is empty.
- element() -> It is same as peek() method.
To remove element from the Queue
- remove() -> It returns and remove the HEAD element from the Queue. It returns null if Queue is empty.
- poll() -> It returns and remove the HEAD from the Queue.
To access additional feature of the Queue
- size() -> To find the size of Queue.
- contains(element) -> To find Whether element is present in the Queue or not.It has return type boolean.
- isEmpty() -> To check Queue is empty or not. it returns true it it is empty.
Example
import java.util.*;
public class QueueDemo{
public static void main(String []args){
Queue<String> queue = new LinkedList<>();
// add element in the Queue
queue.add("Year 2020");
System.out.println("Elements in the queue :-> "+queue);
// add element in the Queue using method offer()
queue.offer("January");
System.out.println("Elements in the queue :-> "+ queue);
// size of the Queue
int size = queue.size();
System.out.println("Size of queue :-> "+ size );
queue.offer("February");
queue.offer("Corona");
System.out.println("Elements in the queue :-> "+ queue);
// To check element is present in the Queue or not
boolean isContains = queue.contains("Corona");
if(isContains){
// remove element from the Queue
String element = queue.remove();
System.out.println(element + " element is removed from the history");
System.out.println("Elements in the queue :-> "+ queue);
}
// fetch element from the Queue but it will not remove
String head = queue.peek();
System.out.println("Now HEAD element in the queue :-> "+ head);
// remove element from the Queue using poll()
String element = queue.poll();
System.out.println(element + " element is removed from the history");
System.out.println("Elements in the queue :-> "+ queue);
head = queue.peek();
System.out.println("Now HEAD element in the queue :-> "+ head);
element = queue.poll();
System.out.println(element + " element is removed from the history");
System.out.println("Elements in the queue :-> "+ queue);
head = queue.element();
System.out.println("Now HEAD element in the queue :-> "+ head);
queue.poll();
// check whether Queue is empty or not
boolean isEmpty = queue.isEmpty();
if(isEmpty)
System.out.println("Congratulations!! Corona is gone from the earth ");
else
System.out.println("Oh!! there is still some viruses ");
System.out.println("Elements in the queue :-> "+ queue);
}
}
Output
![]() |
Output Of Queue Code |
Thanks!!!
May 16, 2020
Tags :
Data Structure
Subscribe by Email
Follow Updates Articles from This Blog via Email
1 Comments
It's very useful. Learning new things made easy ��
Reply DeletePlease comment here...