LinkedTransferQueue retainAll() method in Java with Examples

The retainAll() function in the Java.util.concurrent package's LinkedTransferQueue class is used to keep only the queue's elements that are part of the designated collection, this method is especially helpful if you have two collections and only want to keep the elements that are shared by both. It is especially helpful in the case when the tasks are being completed based on the simultaneous queues and collections, thus, you are making the changes to the queue in a thread-safe manner.

Method Signature

Explanation

Method Name: retainAll: It is the name of the method that performs the operation of keeping the elements in the queue based on the collection of elements.

Return Type: boolean: The return type is boolean, which tells that the queue was either modified or not due to the operation. The function returns true if the queue was changed. The function returns false if the queue is not changed.

Parameter: C: The parameter C represents a collection containing elements to be retained in the queue.

Example 1: Retain Elements from a Collection

In this example, we start by creating a LinkedTransferQueue named queue and add elements [1, 2, 3, 4] to it. Then, we create another LinkedTransferQueue named retainCollection and add elements [2, 4] to it. We call queue.retainAll(retainCollection) to retain only the elements present in both queue and retainCollection. After the operation, the queue queue will contain only elements [2, 4] as they are common between queue and retainCollection. The retainAll() method returns true indicating that the queue was modified.

Filename: RetainElementsExample.java

Output

Original Queue: [1, 2, 3, 4]
Collection to Retain: [2, 4]
Modified: true
Queue after retainAll: [2, 4]                    

Example 2: Retain Strings from a List

In this example, we'll demonstrate how to use the retainAll() method of LinkedTransferQueue to retain only the elements present in both the queue and a List of strings, this example focuses on retaining strings, but the same principle applies to retaining elements of any type from a collection.

Filename: RetainStringExample1.java

Output

Original Fruit Queue: [apple, banana, orange, pear]
Selected Fruits: [banana, orange]
Modified: true
Queue after retainAll: [banana, orange]

Example 3: Retain Elements from Another Queue

In this example, we create two LinkedTransferQueues, queue1 and queue2, containing integers. queue1 contains elements [1, 2, 3] while queue2 contains [2, 3, 4]. We call queue1.retainAll(queue2) to retain only the elements from queue1 that are also present in queue2. After the operation, queue1 will contain only elements [2, 3], which are common between queue1 and queue2. The retainAll() method returns true, indicating that queue1 was modified.

Filename: RetainFromAnotherQueueExample.java

Output

Queue1: [1, 2, 3]
Queue2: [2, 3, 4]
Modified: true
Queue1 after retainAll: [2, 3]

Example 4: Retain Even Numbers from Two Queues

In this example, we create two LinkedTransferQueues, queue1 and queue2, containing integers. queue1 contains elements [1, 2, 3] while queue2 contains [2, 3, 4]. We call queue1.retainAll(queue2) to retain only the even numbers from queue1 that are also present in queue2. After the operation, queue1 will contain only the even number 2, which is common between queue1 and queue2. The retainAll() method modifies queue1 in place and returns true, indicating that queue1 was modified.

Filename: RetainEvenNumbersExample.java

Output

Original Queue1: [1, 2, 3]
Original Queue2: [2, 3, 4]
Queue1 after retaining even numbers from Queue2: [2]

Example 5: Retain Common Strings from Two Queues

In this example, we create two LinkedTransferQueues, queue1 and queue2, containing strings. queue1 initially contains elements ["apple", "banana", "orange"] and queue2 contains elements ["banana", "orange", "pear"]. We call queue1.retainAll(queue2) to retain only the strings from queue1 that are also present in queue2. After the operation, queue1 will contain only the strings "banana" and "orange", which are common between queue1 and queue2. The retainAll() method modifies queue1 in place and returns true, indicating that queue1 was modified.

Filename: RetainCommonStringsExample.java

Output

Original Queue1: [apple, banana, orange]
Original Queue2: [banana, orange, pear]
Queue1 after retaining common strings: [banana, orange]

Example 6: Retain Prime Numbers from Two Queues

In this example, we create two LinkedTransferQueues, queue1 and queue2, containing integers. We iterate through each element in queue1 and check if it's present in queue2 and if it's a prime number. If both conditions are met, we add the number to a temporary queue. After iterating through all elements, we clear queue1 and add all elements from the temporary queue to it. As a result, queue1 will contain only the prime numbers 2 and 3, which are present in both queues.

Filename: RetainPrimeNumbersExample.java

Output

Original Queue1: [1, 2, 3, 4]
Original Queue2: [2, 3, 5, 7]
Queue1 after retaining prime numbers from Queue2: [2, 3]