About 31,200,000 results
Open links in new tab
  1. PriorityQueue in Java - GeeksforGeeks

    Apr 15, 2025 · Let’s see how to perform a few frequently used operations on the Priority Queue class. 1. Adding Elements. To add an element in a priority queue, we can use the add() method. The insertion order is not retained in the PriorityQueue. The elements are stored based on the priority order which is ascending by default. Example: Java

  2. java - How do I use a PriorityQueue? - Stack Overflow

    Mar 25, 2009 · Here's an example of a priority queue sorting by string length: public static void main(String[] args) { Comparator<String> comparator = new StringLengthComparator(); PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator); queue.add("short"); queue.add("very long indeed"); queue.add("medium"); while (queue.size() != 0) {

  3. Guide to Java PriorityQueue - Baeldung

    Jan 8, 2024 · In this short tutorial, we’ll talk about the Java implementation of the Priority Queue. First, we‘ll see the standard usage and present some examples by ordering the queue in natural and inverse order.

  4. Priority Queues in Java Explained with Examples

    Jul 6, 2020 · Priority queues help consumers consume the higher priority messages first followed by the lower priority messages. Now let's see some actual Java code that will show us how to use priority queues. Here is some code showing how to create a simple priority queue for strings. testStringsPQ.add("abcd"); . testStringsPQ.add("1234"); .

  5. Java PriorityQueue - Programiz

    In order to create a priority queue, we must import the java.util.PriorityQueue package. Once we import the package, here is how we can create a priority queue in Java. PriorityQueue<Integer> numbers = new PriorityQueue<>(); Here, we have created a priority queue without any …

  6. Using PriorityQueue to Have Sorted Collection of Data in Java

    Jan 1, 2023 · Let’s walk through some code examples of how a priority queue can be used to order elements in ascending or descending order. 1. Sorting elements in ascending order in the priority queue. A comparator class allows us to define how elements are ordered in the queue. To sort the elements in ascending order we need to define the queue as below.

  7. Java Priority Queue (+ Comparator Example) - HowToDoInJava

    Aug 4, 2023 · Learn to create, use and understand how a priority queue works in Java. We will examples of queues with elements stored in natural order as well as custom order using Comparator instance.

  8. Java Priority Queue Tutorial – Implementation & Examples

    Apr 1, 2025 · This Tutorial Explains the Java Priority Queue and related Concepts like Comparator, Min and Max Priority Queue along with its Implementation and Examples.

  9. Java PriorityQueue: Ordering and Organizing Elements

    Oct 25, 2023 · In this guide, we’ll walk you through the process of using Java’s Priority Queue, from basic usage to advanced techniques. We’ll cover everything from creating a PriorityQueue, adding elements to it, to more complex uses such as using a custom Comparator to determine the order of elements.

  10. Java Priority Queue Tutorial with Examples - CalliCoder

    In this example, you’ll learn how to create a priority queue of user defined objects. Since a priority queue needs to compare its elements and order them accordingly, the user defined class must implement the Comparable interface, or you must provide a …

  11. Some results have been removed
Refresh