Tutorials Hut

Tutorials Hut

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

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top