• September 1, 2014
  • Core Java
  • Comments Off on Queue interface in java

Queue interface in java

Queue interface:

Queue represents a collection for holding elements prior to processing.

Note: Queue declares its own methods in addition with methods declared in Collection interface.

Commonly used methods of Queue Interface:

1. add(Object obj): Inserts the specified element into this queue. Returns true if queue changed after the operation otherwise returns false.

Syntax: public boolean add(Object obj)

2. offer(Object obj): Inserts the specified element into this queue. Returns true if the element was added to this queue otherwise returns false.

Syntax: public boolean offerr(Object obj)

3. remove(): Returns and removes the head of this queue. It throws an exception if this queue is empty.

Syntax: public Object remove()

4. poll(): Returns and removes the head of this queue. Returns null if this queue is empty.

Syntax: public Object poll()

5. element(): Returns but does not removes the head of this queue. It throws an exception if this queue is empty.

Syntax: public Object element()

6. peek(): Returns but does not removes the head of this queue. Returns null if this queue is empty.

Syntax: public Object peek()

 A simple example of PriorityQueue class to explain few methods of Queue interface.

 PriorityQueueTest.java

import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
 
/**
 * This class is used to show the PriorityQueue functionality.
 * @author javawithease
 */
public class PriorityQueueTest {
	public static void main(String args[]){
		//Create PriorityQueue  object.
		Queue priorityQueue  = new PriorityQueue();
 
		//Add objects to the PriorityQueue .
		priorityQueue.add("Gourav");
		priorityQueue.add("Neeraj");
		priorityQueue.add("Deepak");
		priorityQueue.add("Mohan");
		priorityQueue.add("Parmender");
 
		//Print the PriorityQueue object.
		System.out.println("HasPriorityQueue elements:");
		System.out.println(priorityQueue);
 
		//Print the PriorityQueue elements using iterator.
		Iterator iterator1=priorityQueue.iterator();
		System.out.println("PriorityQueue elements " +
				"using iterator:");
		while(iterator1.hasNext()){  
		   System.out.println(iterator1.next());  
		}
 
		//Print the head element of the PriorityQueue
		System.out.println("Head element: " 
				+ priorityQueue.element());
		System.out.println("Head element: " 
				+ priorityQueue.peek());
 
		//Remove the head element of the PriorityQueue
		priorityQueue.poll();
		priorityQueue.remove();
 
		//Print the PriorityQueue object.
		System.out.println("HasPriorityQueue elements " +
				"after manipulation:");
		System.out.println(priorityQueue);
 
		//Print the PriorityQueue elements using iterator.
		Iterator iterator2=priorityQueue.iterator();
		System.out.println("PriorityQueue elements after " +
				"manipulation using iterator:");
		while(iterator2.hasNext()){  
		   System.out.println(iterator2.next());  
		}
	}
}

Output:

HasPriorityQueue elements:
[Deepak, Mohan, Gourav, Neeraj, Parmender]
PriorityQueue elements using iterator:
Deepak
Mohan
Gourav
Neeraj
Parmender
Head element: Deepak
Head element: Deepak
HasPriorityQueue elements after manipulation:
[Mohan, Neeraj, Parmender]
PriorityQueue elements after manipulation using iterator:
Mohan
Neeraj
Parmender

Download this example.
 
Next Topic: Deque interface in java with example.
Previous Topic: SortedMap interface in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.