The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method.
Parameters: The parameter element is of the type PriorityQueue and refers to the element to be added to the Queue.
Return Value: The function returns True if the element is successfully added to the PriorityQueue .
Below programs illustrate the Java.util.PriorityQueue.add() method:
Program 1: Adding String elements into the Queue.
PriorityQueue: [4, Geeks, To, Welcome, Geeks]
Program 2: Adding Integer elements into the Queue.
PriorityQueue: [5, 10, 30, 20, 15]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.