The LinkedHashSet.retainAll(Collection<?> c)
method in Java is used to retain only the elements in the LinkedHashSet
that are contained in the specified collection.
Table of Contents
- Introduction
retainAll
Method Syntax- Examples
- Retaining Common Elements in LinkedHashSet
- Handling Collections with No Common Elements
- Conclusion
Introduction
The LinkedHashSet.retainAll(Collection<?> c)
method is a member of the LinkedHashSet
class in Java. It allows you to modify the LinkedHashSet
so that it retains only those elements that are also contained in the specified collection. This method is useful for finding the intersection of two collections.
retainAll Method Syntax
The syntax for the retainAll
method is as follows:
public boolean retainAll(Collection<?> c)
- The method takes a single parameter
c
of typeCollection<?>
, which represents the collection containing elements to be retained in theLinkedHashSet
. - The method returns a boolean value:
true
if theLinkedHashSet
changed as a result of the call.false
if theLinkedHashSet
did not change (i.e., it already contained only the elements in the specified collection).
Examples
Retaining Common Elements in LinkedHashSet
The retainAll
method can be used to retain only the elements in the LinkedHashSet
that are also in the specified collection.
Example
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class RetainAllExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
// Creating a list of animals to retain
ArrayList<String> animalsToRetain = new ArrayList<>();
animalsToRetain.add("Lion");
animalsToRetain.add("Giraffe");
// Retaining only the elements that are also in the list
boolean isChanged = animals.retainAll(animalsToRetain);
// Printing the result of retainAll and the LinkedHashSet
System.out.println("Was the LinkedHashSet changed? " + isChanged);
System.out.println("LinkedHashSet after retainAll: " + animals);
}
}
Output:
Was the LinkedHashSet changed? true
LinkedHashSet after retainAll: [Lion]
Handling Collections with No Common Elements
If the specified collection has no common elements with the LinkedHashSet
, the retainAll
method will remove all elements from the set.
Example
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class RetainAllNoCommonExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
// Creating a list of animals to retain with no common elements
ArrayList<String> animalsToRetain = new ArrayList<>();
animalsToRetain.add("Giraffe");
animalsToRetain.add("Monkey");
// Retaining only the elements that are also in the list
boolean isChanged = animals.retainAll(animalsToRetain);
// Printing the result of retainAll and the LinkedHashSet
System.out.println("Was the LinkedHashSet changed? " + isChanged);
System.out.println("LinkedHashSet after retainAll: " + animals);
}
}
Output:
Was the LinkedHashSet changed? true
LinkedHashSet after retainAll: []
Conclusion
The LinkedHashSet.retainAll(Collection<?> c)
method in Java provides a way to retain only those elements in a LinkedHashSet
that are contained in another collection. By understanding how to use this method, you can efficiently find the intersection of two collections and manage the elements in your sets. This method is useful for filtering collections based on specific criteria, making it a valuable tool for collection management in your Java applications.