The Queue interface basically orders the element in FIFO(First In First Out)manner.
Methods of Queue Interface :
- public boolean add(object);
- public boolean offer(object);
- public remove();
- public poll();
- public element();
- public peek();
PriorityQueue class:
The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner.
Example of PriorityQueue:
- 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());
- }
-
- }
- }
Output:head:Amit
head:Amit
iterating the queue elements:
Amit
Jai
Karan
Vijay
Rahul
after removing two elements:
Karan
Rahul
Vijay
No comments:
Post a Comment