Open In App

AbstractCollection retainAll() method in Java with Examples

The retainAll() method in AbstractCollection helps in retaining elements of the specified collection from another collection and removes the unmatched elements from the result.

Syntax:



public boolean retainAll(Collection collection)

Parameters: This method accepts a parameter collection which is the collection containing elements that are needed to be retained.

Return Value: The method returns a boolean value. It returns “true” if the elements in the collections are retained successfully and if they aren’t, “false” value is returned.



Exceptions: This method throws following exceptions:

Below are some examples to illustrate the use of retainAll() method:

Program 1




// Java program to illustrate retainAll() method
  
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String[] args)
    {
        // Creating an arraylist
        AbstractCollection<Object> set1
            = new ArrayList<Object>();
  
        // adding values in set 1 list
        set1.add("one");
        set1.add("two");
        set1.add("three");
  
        // creating another arraylist
        AbstractCollection<Object> set2
            = new ArrayList<Object>();
  
        // adding values in set 2 list
        set2.add("three");
        set2.add("one");
        set2.add("five");
  
        // before invoking retainAll()
        System.out.println("Set 1 contains:\n"
                           + set1 + "\n");
        System.out.println("Set 2 contains:\n"
                           + set2 + "\n");
  
        // invoking retainAll()
        set2.retainAll(set1);
  
        // after invoking retainAll()
        System.out.println("Set 2 after"
                           + " invoking retainAll() method:\n"
                           + set2);
    }
}

Output:
Set 1 contains:
[one, two, three]

Set 2 contains:
[three, one, five]

Set 2 after invoking retainAll() method:
[three, one]

Program 2: To show NullPointerException




// Java program to illustrate retainAll() method
  
import java.util.*;
  
public class NullPointerExample {
    public static void main(String[] args)
    {
  
        // Creating an arraylist
        // and assigning null to it
        AbstractCollection<Object> set1 = null;
  
        // creating another arraylist
        AbstractCollection<Object> set2
            = new ArrayList<Object>();
  
        // adding values in set 2 list
        set2.add("one");
        set2.add("two");
        set2.add("three");
  
        // before invoking retainAll()
        System.out.println("Set 1 contains:"
                           + set1 + "\n");
        System.out.println("Set 2 contains:"
                           + set2 + "\n");
  
        try {
            // invoking retainAll()
            set2.retainAll(set1);
  
            // after invoking retainAll()
            System.out.println("Set 2 after invoking "
                               + "retainAll() method:\n"
                               + set2);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:
Set 1 contains:null

Set 2 contains:[one, two, three]

java.lang.NullPointerException

Article Tags :