Java ArrayList retainAll() Method with Example

In the previous article we have discussed about Java ArrayList containsAll() Method with Example

In this article we are going to see the use of ArrayList retainAll() method along with suitable examples by using Java programming language.

Java ArrayList retainAll() Method with Example

retainAll():

This java.util.ArrayList.retainAll() method is used to keep only the common elements from the two collections.

It returns true if all the element of one collection is present in another collection else it returns false.

If the type of collection is incompatible with another collection then it throws ClassCastException.

Syntax:

arrayListName.retainAll(Collection col)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Collection col refers to the collection which contains elements that to be retained in the list.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList retainAll() Method – Example with String Type ArrayList

Approach:

  • Create 2 new ArrayList of type String.
  • Add string elements into both ArrayList using the add() method.
  • Display the ArrayList elements.
  • Now by using retainAll() method display the list which are contained in the specified collection.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("mi");
        arr.add("vivo");
        arr.add("samsung");
        arr.add("nokia");
        arr.add("black berry");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 1 are: "+arr);
        // Create a ArrayList of string datatype
        ArrayList<String> arr1 = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr1.add("vivo");
        arr1.add("htc");
        arr1.add("samsung");
        arr1.add("realme");
        arr1.add("nokia");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 2 are: "+arr1);
        // retrive the common elements
        arr.retainAll(arr1);
        System.out.println("common elements in List 1 are: " +arr);
        arr1.retainAll(arr);
        System.out.println("common elements in List 2 are: " +arr1);
    }
}
Output:

The elements of ArrayList 1 are: [mi, vivo, samsung, nokia, black berry]
The elements of ArrayList 2 are: [vivo, htc, samsung, realme, nokia]
common elements in List 1 are: [vivo, samsung, nokia]
common elements in List 2 are: [vivo, samsung, nokia]

Method-2: Java ArrayList retainAll() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Now by using retainAll() method display the list which are contained in the specified collection.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 1 are: "+arr);
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr1.add(1);
        arr1.add(100);
        arr1.add(84);
        arr1.add(17);
        arr1.add(0);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 2 are: "+arr1);
        // retrive the common elements
        arr.retainAll(arr1);
        System.out.println("common elements in List 1 are: " +arr);
        arr1.retainAll(arr);
        System.out.println("common elements in List 2 are: " +arr1);
    }
}
Output:

The elements of ArrayList 1 are: [2, 52, 13, 17, 1]
The elements of ArrayList 2 are: [1, 100, 84, 17, 0]
common elements in List 1 are: [17, 1]
common elements in List 2 are: [1, 17]

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: