Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaCopyOnWriteArrayList retainAll() method in Java with Examples

CopyOnWriteArrayList retainAll() method in Java with Examples

The Java.util.concurrent.CopyOnArrayList.retainAll() method in Java is used to retain only the elements in the list that are contained in specific collection.

Syntax:

 public boolean retainAll(Collection col) 

Parameters: This method accepts a mandatory parameter col which is of the type of collection. This is the collection of elements to be retained in this vector.

Return Type: This method returns a boolean value true if the list is changed at all as a result of call else false.

Exception: This method throws a NullPointerException if the specified collection is NULL.

Below Programs illustrate the retainAll() method of CopyOnArrayList in Java:

Program 1: This program involves CopyOnArraylist retainAll() method of Integer Type:




// Java Program to illustrate CopyOnArrayList
// retainAll() method
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis1
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis1.add(34);
        ArrLis1.add(66);
        ArrLis1.add(72);
        ArrLis1.add(10);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1);
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis2
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis2.add(10);
        ArrLis2.add(66);
        ArrLis2.add(97);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2);
  
        // Applying the function
        ArrLis2.retainAll(ArrLis1);
  
        // print CopyOnWriteArrayLists
  
        System.out.println("After applying retainAll()"
                           + " method on ArrLis2");
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1);
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2);
    }
}


Output:

CopyOnWriteArrayList1: [34, 66, 72, 10]
CopyOnWriteArrayList2: [10, 66, 97]
After applying retainAll() method on ArrLis2
CopyOnWriteArrayList1: [34, 66, 72, 10]
CopyOnWriteArrayList2: [10, 66]

Program 2: This program involves CopyOnArraylist retainAll() method of String Type:




// Java Program to illustrate CopyOnArrayList
// retainAll() method
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        CopyOnWriteArrayList<String> ArrLis1
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis1.add("geeks");
        ArrLis1.add("shaan");
        ArrLis1.add("gfg");
        ArrLis1.add("programming");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1);
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<String> ArrLis2
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis2.add("geeks");
        ArrLis2.add("gfg");
        ArrLis2.add("qwe");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2);
  
        // Applying the function
        ArrLis2.retainAll(ArrLis1);
  
        // print CopyOnWriteArrayLists
  
        System.out.println("After applying retainAll()"
                           + " method on ArrLis2");
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1);
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2);
    }
}


Output:

CopyOnWriteArrayList1: [geeks, shaan, gfg, programming]
CopyOnWriteArrayList2: [geeks, gfg, qwe]
After applying retainAll() method on ArrLis2
CopyOnWriteArrayList1: [geeks, shaan, gfg, programming]
CopyOnWriteArrayList2: [geeks, gfg]
RELATED ARTICLES

Most Popular

Recent Comments