LinkedTransferQueue retainAll() method in Java with Examples6 Jan 2025 | 6 min read 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 SignatureExplanation 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 CollectionIn 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 ListIn 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 QueueIn 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 QueuesIn 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 QueuesIn 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 QueuesIn 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] |
? Sometimes, we are required to represent data in tabular format or in table form, especially when we are dealing with the database. In this section, we will create Java programs that print data on the console in tabular or table format. There are many ways to print...
4 min read
| Java Program to Short an Array of 0's, 1's, and 2's Dutch National Flag (DNF) problem is one of the most popular programming problems proposed by the famous Dutch computer scientist Edsger Dijkstra. As its name suggest, it is based on the flag of Netherlands...
7 min read
Find whether an array of strings may be linked together to create a circle. If the last character of string X and the first character of string Y are the same, then string X can be positioned in a circle before string Y. Example 1: Input: String a =...
7 min read
? In the world of Java programming, streams have emerged as a powerful and versatile concept for processing collections of data in a concise and efficient manner. Introduced in Java 8, streams provide a functional approach to working with data, allowing developers to perform complex operations on...
7 min read
In this section, we have created a number of Java program to check if a given number is perfect square or not. The perfect square or square number is a positive integer that is square of an integer. In other words, when we multiply two same numbers...
6 min read
? In Java, the ArrayList is a widely used data structure that allows dynamic resizing of elements. When it comes to displaying the contents of an ArrayList, the default behavior is to print the elements enclosed within square brackets. However, there are scenarios where you might want...
5 min read
The shift operator is a special type of operator used for bit manipulation of data. It shifts the bits of their first operand left to right or right to left. There are the following three types of shift operators available in Java: Right-Shift Operator (>>) Left-Shift Operator (<<) Unsigned...
5 min read
Deserialization, commonly called unmarshalling, returns serialized data to its original form so that an application may utilize it. Through the Serializable and Externalizable interfaces, which are used to turn objects into streams of bytes and back again, Java supports serialization and deserialization. Custom Serialisation By implementing the Serializable...
4 min read
A number n can be said to be a straight-line number if the digits of the number form an arithmetic progression. It is evident that to decide whether the digits are in arithmetic progression or not. There is a requirement of at least three digits. Therefore,...
7 min read
In Java, LinkedTransferQueue is a concurrent queue implementation which possesses the combination of characteristics from both the traditional blocking queues and the direct handoff queues. It implements the TransferQueue interface that is extending the BlockingQueue class and extends the functionalities of the producer-consumer scenario through...
14 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India