Preface
In Java,retainAll()
yesCollectionInterface (List
、Set
etc. set classes implement this interface) a method used to retain elements in the set that intersect with the specified set and delete all other elements.
The following is correctretainAll()
Detailed explanation of the method.
1. Method definition
Method signature
boolean retainAll(Collection<?> c)
parameter
-
c
: A collection that specifies the elements to be preserved.
Return value
- Return one
boolean
Value:-
true
: If the content of the collection changes due to calling this method. -
false
: If the content of the collection has not changed (i.e. the elements in the collection are the same before and after calling this method).
-
2. Function description
-
retainAll()
The method will call the set of the method (assuming it isA
All elements and parameter sets in ) (assuming it isB
) compare and retain the elements that intersect the two. - if
A
The element in it is not thereB
, they will be removed. - Parameter set
B
Will not be modified.
3. Use examples
Basic usage
import ; import ; public class RetainAllExample { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<>((1, 2, 3, 4, 5)); ArrayList<Integer> list2 = new ArrayList<>((3, 4, 5, 6, 7)); // Keep the intersection of list1 and list2 (list2); (list1); // Output: [3, 4, 5] } }
Return value example
import ; import ; public class RetainAllExample { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(("A", "B", "C")); ArrayList<String> list2 = new ArrayList<>(("B", "C")); boolean isModified = (list2); (isModified); // Output: true, because list1 has changed (list1); // Output: [B, C] } }
4. Common scenarios
Scene 1: Find the intersection of two sets
useretainAll()
You can quickly find the intersection of two sets:
Set<Integer> set1 = new HashSet<>((1, 2, 3, 4)); Set<Integer> set2 = new HashSet<>((3, 4, 5, 6)); (set2); (set1); // Output: [3, 4]
Scene 2: Remove unwanted elements from the collection
By comparing with a known set, the required elements are preserved and the rest are deleted:
List<String> fruits = new ArrayList<>(("Apple", "Banana", "Mango", "Orange")); List<String> preferredFruits = ("Apple", "Orange"); (preferredFruits); (fruits); // Output: [Apple, Orange]
5. Precautions and FAQs
Things to note
-
null collection not supported:
- If the parameter set is
null
, callretainAll()
Will be thrownNullPointerException
。List<Integer> list = new ArrayList<>((1, 2, 3)); (null); // Throw NullPointerException
- If the parameter set is
-
The original collection will be modified:
- After calling this method, the content of the original collection will be changed, leaving only the intersection elements.
-
Parameter set cannot be modified:
-
retainAll()
The parameter set will not be modified.
-
FAQ
-
Air intersection:
If the two sets have no intersection, thenretainAll()
After that, the original collection will become empty.List<Integer> list1 = new ArrayList<>((1, 2, 3)); List<Integer> list2 = new ArrayList<>((4, 5, 6)); (list2); (list1); // Output: []
-
Retain in sequence:
If usedList
(likeArrayList
),retainAll()
When the intersection is preserved, the order of elements is in the order of the original set.List<Integer> list1 = new ArrayList<>((1, 2, 3, 4)); List<Integer> list2 = new ArrayList<>((4, 3)); (list2); (list1); // Output: [3, 4]
6. Internal Principles
Execution process
- Traversal call
retainAll()
A collection of (such aslist1
)。 - For each element, check if it exists in the parameter set (e.g.
list2
)。 - If it does not exist, remove the element.
- return
true
, if at least one element is removed; otherwise returnfalse
。
efficiency
- Depends on the type of parameter set:
- If the parameter set is a
HashSet
,retainAll()
The performance is better becauseHashSet
Provides a quick lookup operation (O(1)). - If the parameter set is a
List
, performance may be lower because the lookup operation requires linear time (O(n)).
- If the parameter set is a
7. Differences from other methods
removeAll()
-
the difference:
removeAll()
is to delete elements in the current set that intersect with the specified set, andretainAll()
It is the retained intersection element. -
Example:
List<Integer> list1 = new ArrayList<>((1, 2, 3, 4)); List<Integer> list2 = new ArrayList<>((3, 4, 5)); // removeAll delete the intersection(list2); (list1); // Output: [1, 2] // retainAll retains intersectionlist1 = new ArrayList<>((1, 2, 3, 4)); (list2); (list1); // Output: [3, 4]
8. Summary
method | describe |
---|---|
effect | Keep elements in the set that intersect with the specified set, and the remaining elements are deleted |
Modify the original collection | yes |
Parameters cannot be empty | If the parameter set is empty, it will be thrown.NullPointerException
|
Return value | If the set changes, returntrue , otherwise returnfalse
|
Common uses | Find intersections and filter elements in collections |
retainAll()
It is a common method for operating collections, which can help developers quickly perform intersection operations between collections. When using it, it is necessary to note that the original collection will be modified, so in some scenarios, the original collection data may need to be backed up to avoid data loss.
This is all about this article about the Java retainAll() method. For more related contents of the Java retainAll() method, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!