The Queue interface in Java represents a first-in, first-out (FIFO) collection of objects. A Queue is used to hold elements that need to be processed in the order they were inserted. Since Queue is an interface, concrete implementations like LinkedList and PriorityQueue must be used to create Queue objects. Common Queue methods include add(), remove(), peek(), and poll() to insert and retrieve elements from the front of the queue.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
31 views7 pages
Javaqueue 220330101003
The Queue interface in Java represents a first-in, first-out (FIFO) collection of objects. A Queue is used to hold elements that need to be processed in the order they were inserted. Since Queue is an interface, concrete implementations like LinkedList and PriorityQueue must be used to create Queue objects. Common Queue methods include add(), remove(), peek(), and poll() to insert and retrieve elements from the front of the queue.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7
Java Queue
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. Creating Queue Objects Since 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. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the Queue. This type-safe queue can be defined as: Queue Interface declaration Method Description
boolean add(object) It is used to insert the specified
element into this queue and return true upon success. boolean offer(object) It is used to insert the specified element into this queue. Object remove() It is used to retrieves and removes the head of this queue. Object poll() It is used to retrieves and removes the head of this queue, or returns null if this queue is empty. Object element() It is used to retrieves, but does not remove, the head of this queue. Object peek() It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. Java PriorityQueue Example import java.util.*; class TestCollection12{ public static void main(String args[]){ PriorityQueue<String> queue=new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); queue.add("Jai"); queue.add("Rahul"); System.out.println("head:"+queue.element()); System.out.println("head:"+queue.peek()); System.out.println("iterating the queue elements:"); Iterator itr=queue.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } queue.remove(); queue.poll(); System.out.println("after removing two elements:"); Iterator<String> itr2=queue.iterator(); while(itr2.hasNext()){ System.out.println(itr2.next()); } } } Characteristics of a Queue: The Queue is used to insert elements at the end of the queue and removes from the beginning of the queue. It follows FIFO concept. The Java Queue supports all methods of Collection interface including insertion, deletion etc. LinkedList, ArrayBlockingQueue and PriorityQueue are the most frequently used implementations. If any null operation is performed on BlockingQueues, NullPointerException is thrown. The Queues which are available in java.util package are Unbounded Queues. The Queues which are available in java.util.concurrent package are the Bounded Queues. All Queues except the Deques supports insertion and removal at the tail and head of the queue respectively. The Deques support element insertion and removal at both ends.