YouTip LogoYouTip

Arrays Retainall

## Finding the Intersection of Arrays in Java Using `retainAll()` In Java, finding the intersection of two collections (retaining only the elements that are common to both) is a frequent task. While standard Java arrays do not have a built-in intersection method, the `java.util.Collection` interface provides a powerful method called `retainAll()`. By converting arrays to collections (such as `ArrayList`), you can easily compute their intersection. --- ### Understanding the `retainAll()` Method The `retainAll(Collection c)` method is defined in the `java.util.Collection` interface. * **Behavior:** It retains only the elements in the invoking collection that are contained in the specified collection `c`. In other words, it removes all elements from the target collection that are not present in the parameter collection. * **Return Value:** It returns `true` if the invoking collection changed as a result of the call, and `false` if it did not change (i.e., all elements in the invoking collection were already present in the parameter collection). * **In-Place Modification:** Note that `retainAll()` modifies the collection on which it is called. If you need to preserve the original collection, you should create a copy before calling this method. --- ### Code Example The following example demonstrates how to use `ArrayList` and the `retainAll()` method to find the intersection of two sets of elements. ```java import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create the first list (array1) ArrayList objArray = new ArrayList<>(); // Create the second list (array2) ArrayList objArray2 = new ArrayList<>(); // Populate the second list objArray2.add(0, "common1"); objArray2.add(1, "common2"); objArray2.add(2, "notcommon"); objArray2.add(3, "notcommon1"); // Populate the first list objArray.add(0, "common1"); objArray.add(1, "common2"); objArray.add(2, "notcommon2"); // Display the initial elements of both lists System.out.println("array1 elements: " + objArray); System.out.println("array2 elements: " + objArray2); // Retain only the elements in objArray that are also in objArray2 objArray.retainAll(objArray2); // Display the intersection result System.out.println("Intersection of array1 & array2: " + objArray); } } ``` ### Output When you run the program, it produces the following output: ```text array1 elements: [common1, common2, notcommon2] array2 elements: [common1, common2, notcommon, notcommon1] Intersection of array1 & array2: [common1, common2] ``` --- ### Key Considerations 1. **Destructive Operation:** The `retainAll()` method modifies the source collection (`objArray` in the example above). If you need to keep the original collection intact, instantiate a temporary collection first: ```java ArrayList intersection = new ArrayList<>(objArray); intersection.retainAll(objArray2); ``` 2. **Performance:** The time complexity of `retainAll()` on an `ArrayList` is $O(N \times M)$ in the worst case because `ArrayList.contains()` runs in $O(M)$ time. If you are working with large datasets, consider using a `HashSet` instead of an `ArrayList`, which reduces the lookup time to $O(1)$, making the overall intersection operation $O(N)$. 3. **Null Pointer Exception:** If the collection passed as a parameter to `retainAll()` is `null`, the method will throw a `NullPointerException`.
← Date Time Am PmArrays Removeall β†’