A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/util/java_util_priorityqueue.htm below:

Java PriorityQueue Class

Java PriorityQueue Class Introduction

The Java PriorityQueue class is an unbounded priority queue based on a priority heap. Following are the important points about PriorityQueue −

Class declaration

Following is the declaration for java.util.PriorityQueue class −

public class PriorityQueue<E>
   extends AbstractQueue<E>
   implements Serializable
Parameters

Following is the parameter for java.util.PriorityQueue class −

E − This is the type of elements held in this collection.

Class constructors Sr.No. Constructor & Description 1

PriorityQueue()

This creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.

2

PriorityQueue(Collection<? extends E> c)

This creates a PriorityQueue containing the elements in the specified collection.

3

PriorityQueue(int initialCapacity)

This creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.

4

PriorityQueue(int initialCapacity, Comparator<? super E> comparator)

This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

5

PriorityQueue(PriorityQueue<? extends E> c)

This creates a PriorityQueue containing the elements in the specified priority queue.

6

PriorityQueue(SortedSet<? extends E> c)

This creates a PriorityQueue containing the elements in the specified sorted set.

Class methods Methods inherited

This class inherits methods from the following classes −

Adding an Item to a Priority Queue Example

The following example shows the usage of Java PriorityQueue add(E) method to add Integers. We're adding couple of Integers to the PriorityQueue object using add() method calls per element and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue with an initial capacity
      PriorityQueue<Integer> queue = new PriorityQueue<>(5);

      // use add() method to add elements in the queue
      queue.add(20);
      queue.add(30);
      queue.add(20);
      queue.add(30);
      queue.add(15);
      queue.add(22);
      queue.add(11);

      // let us print all the elements available in queue
      for (Integer number : queue) {
         System.out.println("Number = " + number);
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Number = 11
Number = 20
Number = 15
Number = 30
Number = 30
Number = 22
Number = 20

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4