Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java Collection: Queue and PriorityQueue
Queue is an interface of collections packaged which extends Collection interface and collection interface extends Iterable interface.
Queue implements FIFO (First in first out) order to store and process elements. In this article we will look in details, Java collection Queue and PriorityQueue.
Methods of Queue
Method |
Description |
boolean add(object) |
Adds an element |
boolean offer(object) |
Adds an element in the queue. |
Object remove() |
Removes elements from the queue. |
Object poll() |
Retrieves and removes the head of the queue. |
Object element() |
Only retries elements but does not remove. |
Object peek() |
Only retries elements but does not remove. |
PriorityQueue
PriorityQueue is a class which implements Queue interface and elements are stored and ordered based on their priority. It does not exactly provide FIFO behaviour. Based on the compareTo method of object or order it arranges the elements.
Time complexity O(log(n)) to add or remove elements.
It is not thread safe and hence java provides PriorityBlockingQueue which is thread safe and implements BlockingQueue.
Example of PriorityQueue with add, remove, element, peek methods and iterator
Example 1:
import java.util.*;public class MyCollections{
public static void main(String args[]){
PriorityQueue<String> queue=new PriorityQueue<String>();
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("E");
queue.add("D");
System.out.println("element:"+queue.element());
System.out.println("peek:"+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 1 elements:");
Iterator<String> itr2=queue.iterator();
while(itr2.hasNext()) {
System.out.println(itr2.next());
}
}
Output
element:A
peek:A
iterating the queue elements:
A
B
C
E
D
after removing 1 elements:
C
D
Example 2:
In below example we will see PriorityQueue which has elements of type Truck class.
import java.util.PriorityQueue;class Truck implements Comparable<Truck>{
int number;
String name;
public Truck(int number, String name) {
this.number = number;
this.name = name;
}public int compareTo(Truck truck) {
if(number>truck.number){
return 1;
}else if(number<truck.number){
return -1;
}else{
return 0;
}
}
}class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Truck> queue=new PriorityQueue<Truck>();
//Creating Books
Truck t1=new Truck(115,"Ashok");
Truck t2=new Truck(233,"Vovlo");
Truck t3=new Truck(101,"BMW");//Adding Books to the queue
queue.add(t1);
queue.add(t2);
queue.add(t3);
for(Truck b:queue){
System.out.println(b.number+" "+b.name);
}queue.remove();
System.out.println("After removing one Truck record:");for(Truck b:queue){
System.out.println(b.number+" "+b.name);
}
}
}
Output
101 BMW
233 Vovlo
115 Ashok
After removing one Truck record:
115 Ashok
233 Vovlo
Next Article
- Dqueue and ArrayQueue