JAVA
PriorityQueue
class
The PriorityQueue class provides the facility of using Queue.
But it does not orders the elements in FIFO manner. It inherits AbstractQueue
class.
PriorityQueue Example program
class PriorityQueueExample{
public static void main(String args[]){
PriorityQueue<String> Queue=new
PriorityQueue<String>();
Queue.add("Sindhu");
Queue.add("Deepthi");
Queue.add("Chandu");
Queue.add("Maithili");
Queue.add("sunitha");
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());
}
}
}
OUTPUT
head:Chandu
head:Chandu
iterating the Queue elements:
Chandu
Maithili
Deepthi
Sindhu
sunitha
after removing two elements:
Maithili
Sindhu
sunitha
ArrayDeque
class
The ArrayDeque class provides the facility of using deque and
resizable-array. It inherits AbstractCollection class and implements the Deque
interface.
The important points about ArrayDeque class are:
·
Unlike
Queue, we can add or remove elements from both sides.
·
Null
elements are not allowed in the ArrayDeque.
·
ArrayDeque
is not thread safe, in the absence of external synchronization.
·
ArrayDeque
has no capacity restrictions.
·
ArrayDeque
is faster than LinkedList and Stack.
DeQueue Example program
public class Del {
public static void main(String[] args) {
//Creating Deque and adding
elements
Deque<String> deque = new
ArrayDeque<String>();
deque.add("Deepthi");
deque.add("Chandu");
deque.add("sindhu");
//Traversing elements
for (String str : deque) {
System.out.println(str);
}
}
}
OUTPUT:
Deepthi
Chandu
sindhu