Java ArrayBlockingQueue retainAll() Method

The retainAll() method of Java ArrayBlockingQueue class retains only the elements of the ArrayBlockingQueue that are contained in the specified collection and removes rest of the elements.

Syntax:

Parameters:

Here, the parameter c is the collection containing elements to be retained in the ArrayBlockingQueue collection.

Specified By:

The retainAll () method of ArrayBlockingQueue class is specified by:

Return Value:

The retainAll () method returns a Boolean value 'true' if the collection has changed as a result of the call, else it returns 'false'.

Throws:

It throws NullPointerException, if the specified collection does not support null elements or it contains one or more null elements.

Example 1

Output:

Elements in queue : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Multiple of 5 : [5, 10, 15, 20]

Example 2

Output:

Students in the class : 
Reema
Ranjeeta
Rani
Sukla
Raj
Students failed : 
Ranjeeta
Rani
Raj

Example 3

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.TreeMap.put(TreeMap.java:563)
	at java.util.TreeSet.add(TreeSet.java:255)
	at com.javaTpoint.ArrayBlockingQueueRetainAllExample3.main(ArrayBlockingQueueRetainAllExample3.java:15)

As TreeSet does not permit null elements, so it will give NullPointerException as shown above.