Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the PriorityQueue.addAll() method in Java?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In this shot, we will learn how to use the PriorityQueue.addAll() method in Java.

Introduction

The PriorityQueue.addAll() method is present in the PriorityQueue class inside the java.util package.

PriorityQueue.addAll() is used to insert all the elements specified in the collection in PriorityQueue.

Syntax

The syntax of the PriorityQueue.addAll() function is given below:

public boolean addAll(Collection c);

Parameters

The PriorityQueue.addAll()

  • Collection: A collection with the elements to be inserted in PriorityQueue.

Return value

The PriorityQueue.addAll() method returns a boolean value:

  • True: If the collection is inserted in PriorityQueue.
  • False: If the collection is not inserted in PriorityQueue.

Code

Let us have a look at the code now.

import java.util.*;
class Main
{
public static void main(String[] args)
{
// create PriorityQueue
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
// Add items in PriorityQueue
pq.add(2);
pq.add(28);
pq.add(6);
pq.add(80);
pq.add(28);
pq.add(7);
pq.add(15);
System.out.println("Elements in the priority queue pq are: " + pq);
PriorityQueue<Integer> pq_new = new PriorityQueue<Integer>();
System.out.println("Elements in the new priority queue are: " + pq_new);
pq_new.addAll(pq);
System.out.println("Elements in the new priority queue are: " + pq_new);
}
}
Using the PriorityQueue.addAll() method in Java

Explanation

  • In line 1, we import the required package.

  • In line 2, we make a Main class.

  • In line 4, we make a main() function.

  • In line 6, we declare a PriorityQueue that consists of integer type elements.

  • From lines 8 to 14, we use the PriorityQueue.add() method to insert the elements in PriorityQueue. We will use this collection to create another priority queue.

  • In line 16, we display the elements present in PriorityQueue.

  • In line 18, we declare another PriorityQueue that consists of integer type elements.

  • In line 20, we print the elements present in the new priority queue. As of now, there are no elements.

  • In line 22, we use the PriorityQueue.addAll() method to insert the elements in the PriorityQueue with the pq collection.

  • In line 23, we again display the elements present in the new priority queue. Now, we do get our elements added to the priority queue.

So, in this way, we can use the PriorityQueue.addAll() method in Java to insert a collection of elements in the priority queue data structure.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring