addAll(Collection): This method is available in java.util.PriorityQueue class of Java.
Syntax:
boolean java.util.AbstractQueue.addAll(Collection<? extends K> c)
This method takes one argument. This method adds all of the elements in the specified collection to this queue.
Parameters: One parameter is required for this method.
c: a collection containing elements to be added to this queue.
Returns: true if this queue changed as a result of the call.
Throws:
1. ClassCastException - if the class of an element of the specified collection prevents it from being added to this queue.
2. NullPointerException - if the specified collection contains a null element and this queue does not permit null elements, or if the specified collection is null.
3. IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this queue, or if the specified collection is this queue.
4. IllegalStateException - if not all the elements can be added at this time due to insertion restrictions.
Approach 1: When no exception
Java
import java.util.Collection;import java.util.PriorityQueue;public class PriorityQueueaddAll {public static void main(String[] args) {PriorityQueue<Integer> priorityQueue =new PriorityQueue<Integer>();priorityQueue.add(12);priorityQueue.add(5);priorityQueue.add(10);priorityQueue.add(2);Collection<Integer> collection =new PriorityQueue<Integer>();collection.add(19);collection.add(3);System.out.println(priorityQueue.addAll(collection));}}
Output:
true
Approach 2: NullPointerException
Java
import java.util.Collection;import java.util.PriorityQueue;public class PriorityQueueaddAll {public static void main(String[] args) {PriorityQueue<Integer> priorityQueue =new PriorityQueue<Integer>();priorityQueue.add(12);priorityQueue.add(5);priorityQueue.add(10);priorityQueue.add(2);Collection<Integer> collection = null;System.out.println(priorityQueue.addAll(collection));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.AbstractQueue.addAll(AbstractQueue.java:182)
No comments:
Post a Comment