Java ArrayList retainAll() Method

The ArrayList.retainAll(Collection<?> c) method in Java is used to retain only the elements in the ArrayList that are contained in the specified collection. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. retainAll Method Syntax
  3. Examples
    • Retaining Elements Present in Another Collection
    • Handling Elements Not Present in the ArrayList
    • Handling Empty Collections
  4. Conclusion

Introduction

The ArrayList.retainAll(Collection<?> c) method is a member of the ArrayList class in Java. It allows you to retain only the elements in the ArrayList that are also contained in the specified collection. This method is particularly useful for performing intersection operations on lists.

retainAll Method Syntax

The syntax for the retainAll method is as follows:

public boolean retainAll(Collection<?> c)
  • c: The collection containing elements to be retained in the ArrayList.

The method returns true if the ArrayList was modified as a result of the operation, and false otherwise.

Examples

Retaining Elements Present in Another Collection

The retainAll method can be used to retain only the elements that are present in the specified collection.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RetainAllExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange", "Grapes"));
        List<String> elementsToRetain = Arrays.asList("Banana", "Grapes");

        System.out.println("ArrayList before retainAll: " + list);

        // Retain only elements present in the specified collection
        boolean modified = list.retainAll(elementsToRetain);

        System.out.println("Was the ArrayList modified? " + modified);
        System.out.println("ArrayList after retainAll: " + list);
    }
}

Output:

ArrayList before retainAll: [Apple, Banana, Orange, Grapes]
Was the ArrayList modified? true
ArrayList after retainAll: [Banana, Grapes]

Handling Elements Not Present in the ArrayList

If none of the elements in the specified collection are present in the ArrayList, the retainAll method will remove all elements from the ArrayList.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RetainAllNotPresentExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));
        List<String> elementsToRetain = Arrays.asList("Grapes", "Pineapple");

        // Attempt to retain elements not present in the list
        boolean modified = list.retainAll(elementsToRetain);

        System.out.println("Was the ArrayList modified? " + modified);
        System.out.println("ArrayList after retainAll: " + list);
    }
}

Output:

Was the ArrayList modified? true
ArrayList after retainAll: []

Handling Empty Collections

If the specified collection is empty, the retainAll method will remove all elements from the ArrayList.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RetainAllEmptyCollectionExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));
        List<String> elementsToRetain = Arrays.asList();

        // Attempt to retain elements from an empty collection
        boolean modified = list.retainAll(elementsToRetain);

        System.out.println("Was the ArrayList modified? " + modified);
        System.out.println("ArrayList after retainAll: " + list);
    }
}

Output:

Was the ArrayList modified? true
ArrayList after retainAll: []

Conclusion

The ArrayList.retainAll(Collection<?> c) method in Java is used for retaining only the elements in an ArrayList that are contained in a specified collection. By understanding how to use this method, you can efficiently perform intersection operations on lists in your Java applications. Whether you are retaining elements present in another collection, handling elements not present, or dealing with empty collections, the retainAll method provides a flexible and reliable solution for these tasks.

Comments