Java ArrayList retainAll() Function

retainAll Function in Java

What is an ArrayList?

Arraylist is a part of collection framework in java. It contains some inbuilt function like retainAll() function which can be found in the java.util.ArrayList Package.
It implements the List interface of the collections framework.

Here, in the page we will discuss about the ArrayList’s retainAll() Method in java. To  know more about ArrayList in java, you can click on the button given below.

ArrayList retainAll Function:

Arraylist contains several inbuilt functions that are used to perform several operations on arraylist in Java. The retainAll function is also an inbuilt method of class ArrayList.
The retainAll function removes all those elements of an ArrayList that are not present in the specified class of collection. It retains only those elements in the ArrayList that are similar to the specified class of collection.

Syntax:

arraylist_name.retainAll(Contain c)

Definition of Parameters:

Return Type :

retainAll Function in Java

Example for Retaining Elements of ArrayList of Integers:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<Integer> Prep = new ArrayList<>();
        Prep.add(1);
        Prep.add(2);
        Prep.add(3);
        Prep.add(4);
        
        ArrayList<Integer> Insta = new ArrayList<>();
        Insta.add(5);
        Insta.add(2);
        Insta.add(6);
        Insta.add(3);
        
        System.out.println("ArrayList Prep : " + Prep);
        System.out.println("ArrayList Insta : " + Insta);
        Prep.retainAll(Insta);
        System.out.println("After Performing Operation on Prep : " + Prep);
    }
}
Output:

ArrayList Prep : [1, 2, 3, 4]
ArrayList Insta : [5, 2, 6, 3]
After Performing Operation on Prep : [2, 3]

In the above Example, We had taken two ArrayList “Prep” and “Insta” of integer type, we are passing the ArrayList “Insta” as an argument to the retainAll Function.

Example for Retaining Elements of ArrayList of Strings:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<String> Prep = new ArrayList<>();
        Prep.add("C");
        Prep.add("C++");
        Prep.add("Java");
        Prep.add("Python");
        
        ArrayList<String> Insta = new ArrayList<>();
        Insta.add("Java");
        Insta.add("Python");
        Insta.add("Java Script");
        
        System.out.println("ArrayList Prep : " + Prep);
        System.out.println("ArrayList Insta : " + Insta);
        Prep.retainAll(Insta);
        System.out.println("After Performing Operation on Prep : " + Prep);
    }
}
Output:

ArrayList Prep : [C, C++, Java, Python]
ArrayList Insta : [Java, Python, Java Script]
After Performing Operation on Prep : [Java, Python]

In the above Example. we have created two ArrayLists “Prep” and “Insta” of String Type, we are passing the ArrayList “Insta” as an argument to the retainAll Function.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription