Priority Queue Introduction

priority queue in java

What is a Priority Queue in Java ?

A Priority Queue in java programming is a data structure, in which priority is set already . In priority queue we work with the data depending  on some priority .For example  if we want to delete an element from the queue, the data with the highest priority will be deleted first, in similar way if we are inserting data in the queue, it will be arranged in the queue depending upon its priority.

Let’s see how a priority queue in java works.

Priorty Queue introduction.

Priority Queue is other form of  normal queue with following properties.
  1. Each and Every item has a priority associated with it.
  2. An element which has  high priority is dequeued first as compared with element with low priority.
  3. If by chance elements have the same priority, they are served according to their order in the queue.
  4. A Priority Queue can be implemented using various different data structures like
    • Arrays
    • Linked List
    •  Heap

Priority Queue Operation

  • Enqueue(): This function is for inserting an element in a queue
  • Dequeue(): This function is used for deleting an element in a queue, based on the priority of the element.
  • show():  This will print all elements of the Queue.
priority queue insertion in java
priority queue deletion in java

How is a Priority Queue implemented in Java ?

There are three different data structures that we can use to implement Priority Queue in java:

  • Array
  • Linked List
  • Heap

Java code to implement priority queue using queue class 

Run
// Java program to implement priority queue 

// using queue class 

import java.util.*;

public class Main {

    public static void main(String[] args) {

        // Creating a priority queue of strings
        PriorityQueue< String> pq = new PriorityQueue<>();

        pq.add("hello");
        pq.add("hiii");
        pq.add("by");

        System.out.println("Priority Queue is: " + pq);

        String value = pq.peek();

        System.out.println("Element accessed is: " + value);
    }
}
Output :
Priority Queue is : [by, hiii, hello]
Element accessed is : by

APPLICATIONS OF PRIORITY QUEUES :

 Following are some applications of Priority Queue:

  • Dijkstra’s shortest path algorithm.
  • Data Compression
  • Heap Sort
  • Prim’s Algorithm
  • Operating System: Interrupt Handling.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java