Easy Tutorial
❮ Java Operators Thread Getpri ❯

Java ArrayList retainAll() Method

Java ArrayList

The retainAll() method is used to retain only those elements in the arraylist that are also present in the specified collection, effectively removing elements that are not present in the specified collection.

The syntax for the retainAll() method is:

arraylist.retainAll(Collection c);

Note: arraylist is an object of the ArrayList class.

Parameter:

Return Value

Returns true if elements are removed from the arraylist.

Throws a ClassCastException if the elements in the arraylist are incompatible with the elements in the specified collection.

Throws a NullPointerException if the arraylist contains null elements and the specified collection does not allow null elements.

Example

Retaining elements in the specified collection:

Example

import java.util.ArrayList;
class Main {
    public static void main(String[] args){

        // Create an arraylist
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");

        System.out.println("ArrayList 1: " + sites);

        // Create another arraylist
        ArrayList<String> sites2 = new ArrayList<>();

        // Add elements to the arraylist
        sites2.add("Wiki");
        sites2.add("tutorialpro");
        sites2.add("Google");
        System.out.println("ArrayList 2: " + sites2);

        // Retain elements
        sites.retainAll(sites2);
        System.out.println("Retained elements: " + sites);
    }
}

Output:

ArrayList 1: [Google, tutorialpro, Taobao]
ArrayList 2: [Wiki, tutorialpro, Google]
Retained elements: [Google, tutorialpro]

In the above example, we created two arraylists named sites and sites2.

Note this line:

sites.retainAll(sites2);

In the example, we passed the sites2 arraylist as a parameter to the retainAll() method. This method removes from sites the elements that are not present in sites2.

Common elements between ArrayList and HashSet:

Example

import java.util.ArrayList;
import java.util.HashSet;

class Main {
    public static void main(String[] args) {
        // Create an arraylist
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements to the arraylist
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("ArrayList: " + numbers);

        // Create a HashSet object
        HashSet<Integer> primeNumbers = new HashSet<>();

        // Add elements to the HashSet
        primeNumbers.add(2);
        primeNumbers.add(3);
        primeNumbers.add(5);
        System.out.println("HashSet: " + primeNumbers);

        // Retain common elements
        numbers.retainAll(primeNumbers);
        System.out.println("Common elements: " + numbers);
    }
}

Output:

ArrayList: [1, 2, 3]
HashSet: [2, 3, 5]
Common elements: [2, 3]

In this example, we created an ArrayList named numbers and a HashSet named primeNumbers. The retainAll() method is used to retain only the common elements between the two collections.

// Retain common elements in the arraylist
numbers.retainAll(primeNumbers);
System.out.println("Common elements: " + numbers);
}
}

Executing the above program outputs:

ArrayList: [1, 2, 3]
HashSet: [2, 3, 5]
Common elements: [2, 3]

In the above example, we created an ArrayList named numbers and a HashSet named primeNumbers.

Note this line:

numbers.retainAll(primeNumbers);

In the code, the retainAll() method removes all elements in the numbers ArrayList that are not present in the primeNumbers HashSet.

Java ArrayList ```

❮ Java Operators Thread Getpri ❯