1import java.util.ArrayList;
2public class ArrayListRetainAllMethod
3{
4 public static void main(String[] args)
5 {
6 ArrayList<String> al1 = new ArrayList<String>();
7 al1.add("red");
8 al1.add("blue");
9 al1.add("green");
10 // create another ArrayList
11 ArrayList<String> al2 = new ArrayList<String>();
12 al2.add("red");
13 al2.add("green");
14 al2.add("indigo");
15 al2.add("yellow");
16 // printing ArrayList before using retainAll() method
17 System.out.println("ArrayList 1: " + al1);
18 System.out.println("ArrayList 2: " + al2);
19 // applying retainAll() method to al2 passing al1 as parameter
20 al2.retainAll(al1);
21 System.out.println("After Applying retainAll() method to al2: ");
22 System.out.println("ArrayList 1: " + al1);
23 System.out.println("ArrayList 2: " + al2);
24 }
25}