Queue in Java
Queue Interface In Java :
The Queue interface present in the java.util package and extends the Collection interface is used to hold the elements about to be processed in FIFO(First In First Out) order.
It is an ordered list of objects with its use limited to insert elements at the end of the list and deleting elements from the start of the list, (i.e.), it follows the FIFO or the First-In-First-Out principle.
queue needs a concrete class for the declaration and the most common classes are the PriorityQueue and LinkedList in Java.
Queue is an interface, objects cannot be created of the type queue. We always need a class which extends this list in order to create an object
Example :
import java.util.LinkedList;
import java.util.Queue;
public class q {
public static void main(String[] args)
{
Queue<Integer> qu = new LinkedList<>();
for (int i = 0; i < 5; i++)
qu.add(i);
System.out.println("Elements of queue : " + qu);
System.out.println(qu);
int head = qu.peek();
System.out.println("head of queue : " + head);
int size = qu.size();
System.out.println("Size of queue : " + size);
}
}
Comments
Post a Comment