Find common elements in two ArrayLists in Java

11 Dec, 2018
Prerequisite: ArrayList in Java
Given two ArrayLists, the task is to print all common elements in both the ArrayLists in Java .
Examples:
Input: List1 = ["Hii", "Geeks", "for", "Geeks"], 
       List2 = ["Hii", "Geeks", "Gaurav"]
Output: [Hii, Geeks, Geeks]

Input: List1 = ["a", "b", "c", "d", "e", "f"], 
       List2 = ["b", "d", "e", "h", "g", "c"]
Output:[b, c, d, e]

Using Collections.retainAll() methodSyntax:Collections1.retainAll(Collections2)

This method keeps only the common elements
of both Collection in Collection1.
Approach:Get the two ArrayLists.Find the common elements in both the Lists using Collection.retainAll() method. This method keeps only the common elements of both Collection in Collection1.The List 1 now contains the common elements only.Below is the implementation of the above approach:Program: By modifying the contents of List1.// Java Program to find common elements// in two ArrayLists// Using retainAll() method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Find the common elements        list1.retainAll(list2);          // print list 1        System.out.println("Common elements: "                           + list1);    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]
Program 2: By retaining the contents of List1.// Java Program to find common elements// in two ArrayLists// Using retainAll() method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Create ArrayList list3        ArrayList<String>            list3 = new ArrayList<String>(list1);          // Store the comparison output        // in ArrayList list3        list3.retainAll(list2);          // print list 3        System.out.println("Common elements: "                           + list3);    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]
Using Stream filterSyntax:list1.stream()
    .filter(list2::contains)
    .collect(Collectors
    .toList()));

This method returns element if found in second list.
Approach:First create two ArrayList and add values of list.Convert the ArrayList to Stream using stream() method.Set the filter condition to be distinct using contains() method.Collect the filtered values as List using collect() method. This list will be return common element in both list.Print list3Below is the implementation of the above approach:Program:// Java Program to find common elements// in two ArrayLists// Using  Stream filter method  // import ArrayList packageimport java.util.*;import java.util.stream.*;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Find common elements        System.out.print("Common elements: ");        System.out.println(list1.stream()                               .filter(list2::contains)                               .collect(Collectors                                            .toList()));    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]
Naive approach:First create two ArrayList and add values of list.Create a temporary ArrayList to contain common elements.Iterate through the list1 and check if that element is present in the list2 using ArrayList.contains() method.If found, add it to the list3Print the common elements from list3Below is the implementation of the above approach:// Java Program to find common elements// in two ArrayLists// Using  Stream filter method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Create ArrayList list3        ArrayList<String>            list3 = new ArrayList<String>();          // Find common elements        // while iterating through list1        for (String temp : list1) {              // Check if theis element is            // present in list2 or not            if (list2.contains(temp)) {                  // Since present, add it to list3                list3.add(temp);            }        }          // print common elements from list 3        System.out.println("Common elements: "                           + list3);    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]
My Personal Notes
arrow_drop_upSave
Using Collections.retainAll() methodSyntax:Collections1.retainAll(Collections2)

This method keeps only the common elements
of both Collection in Collection1.
Approach:Get the two ArrayLists.Find the common elements in both the Lists using Collection.retainAll() method. This method keeps only the common elements of both Collection in Collection1.The List 1 now contains the common elements only.Below is the implementation of the above approach:Program: By modifying the contents of List1.// Java Program to find common elements// in two ArrayLists// Using retainAll() method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Find the common elements        list1.retainAll(list2);          // print list 1        System.out.println("Common elements: "                           + list1);    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]
Program 2: By retaining the contents of List1.// Java Program to find common elements// in two ArrayLists// Using retainAll() method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Create ArrayList list3        ArrayList<String>            list3 = new ArrayList<String>(list1);          // Store the comparison output        // in ArrayList list3        list3.retainAll(list2);          // print list 3        System.out.println("Common elements: "                           + list3);    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

Syntax:
Collections1.retainAll(Collections2)

This method keeps only the common elements
of both Collection in Collection1.

Approach:
Get the two ArrayLists.Find the common elements in both the Lists using Collection.retainAll() method. This method keeps only the common elements of both Collection in Collection1.The List 1 now contains the common elements only.
Get the two ArrayLists.
Find the common elements in both the Lists using Collection.retainAll() method. This method keeps only the common elements of both Collection in Collection1.
The List 1 now contains the common elements only.
Below is the implementation of the above approach:Program: By modifying the contents of List1.
// Java Program to find common elements// in two ArrayLists// Using retainAll() method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Find the common elements        list1.retainAll(list2);          // print list 1        System.out.println("Common elements: "                           + list1);    }}
List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

Program 2: By retaining the contents of List1.
// Java Program to find common elements// in two ArrayLists// Using retainAll() method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Create ArrayList list3        ArrayList<String>            list3 = new ArrayList<String>(list1);          // Store the comparison output        // in ArrayList list3        list3.retainAll(list2);          // print list 3        System.out.println("Common elements: "                           + list3);    }}
List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

Using Stream filterSyntax:list1.stream()
    .filter(list2::contains)
    .collect(Collectors
    .toList()));

This method returns element if found in second list.
Approach:First create two ArrayList and add values of list.Convert the ArrayList to Stream using stream() method.Set the filter condition to be distinct using contains() method.Collect the filtered values as List using collect() method. This list will be return common element in both list.Print list3Below is the implementation of the above approach:Program:// Java Program to find common elements// in two ArrayLists// Using  Stream filter method  // import ArrayList packageimport java.util.*;import java.util.stream.*;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Find common elements        System.out.print("Common elements: ");        System.out.println(list1.stream()                               .filter(list2::contains)                               .collect(Collectors                                            .toList()));    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

Syntax:
list1.stream()
    .filter(list2::contains)
    .collect(Collectors
    .toList()));

This method returns element if found in second list.

Approach:
First create two ArrayList and add values of list.Convert the ArrayList to Stream using stream() method.Set the filter condition to be distinct using contains() method.Collect the filtered values as List using collect() method. This list will be return common element in both list.Print list3
First create two ArrayList and add values of list.
Convert the ArrayList to Stream using stream() method.
Set the filter condition to be distinct using contains() method.
Collect the filtered values as List using collect() method. This list will be return common element in both list.
Print list3
Below is the implementation of the above approach:
Program:
// Java Program to find common elements// in two ArrayLists// Using  Stream filter method  // import ArrayList packageimport java.util.*;import java.util.stream.*;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Find common elements        System.out.print("Common elements: ");        System.out.println(list1.stream()                               .filter(list2::contains)                               .collect(Collectors                                            .toList()));    }}
List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

Naive approach:First create two ArrayList and add values of list.Create a temporary ArrayList to contain common elements.Iterate through the list1 and check if that element is present in the list2 using ArrayList.contains() method.If found, add it to the list3Print the common elements from list3Below is the implementation of the above approach:// Java Program to find common elements// in two ArrayLists// Using  Stream filter method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Create ArrayList list3        ArrayList<String>            list3 = new ArrayList<String>();          // Find common elements        // while iterating through list1        for (String temp : list1) {              // Check if theis element is            // present in list2 or not            if (list2.contains(temp)) {                  // Since present, add it to list3                list3.add(temp);            }        }          // print common elements from list 3        System.out.println("Common elements: "                           + list3);    }}Output:List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

First create two ArrayList and add values of list.Create a temporary ArrayList to contain common elements.Iterate through the list1 and check if that element is present in the list2 using ArrayList.contains() method.If found, add it to the list3Print the common elements from list3
First create two ArrayList and add values of list.
Create a temporary ArrayList to contain common elements.
Iterate through the list1 and check if that element is present in the list2 using ArrayList.contains() method.
If found, add it to the list3
Print the common elements from list3
Below is the implementation of the above approach:
// Java Program to find common elements// in two ArrayLists// Using  Stream filter method  // import ArrayList packageimport java.util.ArrayList;  public class GFG {      // main method    public static void main(String[] args)    {          // create ArrayList list1        ArrayList<String>            list1 = new ArrayList<String>();          // Add values in ArrayList        list1.add("Hii");        list1.add("Geeks");        list1.add("for");        list1.add("Geeks");          // print list 1        System.out.println("List1: "                           + list1);          // Create ArrayList list2        ArrayList<String>            list2 = new ArrayList<String>();          // Add values in ArrayList        list2.add("Hii");        list2.add("Geeks");        list2.add("Gaurav");          // print list 2        System.out.println("List2: "                           + list2);          // Create ArrayList list3        ArrayList<String>            list3 = new ArrayList<String>();          // Find common elements        // while iterating through list1        for (String temp : list1) {              // Check if theis element is            // present in list2 or not            if (list2.contains(temp)) {                  // Since present, add it to list3                list3.add(temp);            }        }          // print common elements from list 3        System.out.println("Common elements: "                           + list3);    }}
List1: [Hii, Geeks, for, Geeks]
List2: [Hii, Geeks, Gaurav]
Common elements: [Hii, Geeks, Geeks]

Java - util package
Java-Collections
Java-List-Programs
Technical Scripter 2018
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.