Java ConcurrentLinkedQueue retainAll() method

The retainAll() method of ConcurrentLinkedQueue class keeps only those elements in this queue that are present in the defined collection.

Syntax:

Specified By:

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

retainAll() method in interface Collection<E>.

Override:

The retainAll() method of ConcurrentLinkedQueue class is overrided by:

retainAll() method in class AbstractCollection<E>.

Parameters:

c - The collection contains the elements which is to be kept in this collection.

Return Value:

The retainAll() method returns true if this collection changes as a result of the call.

Throws:

The retainAll() method throws NullPointerException if this collection 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:

1.Reema
2.Ranjeeta
3.Rani
4.Sukla
5.Raj
Students failed : 
1.Ranjeeta
2.Rani
3.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.ConcurrentLinkedQueueRetainAllExample3.main(ConcurrentLinkedQueueRetainAllExample3.java:13)