Java Program to Find Common Elements in Two ArrayList


In this article, we will understand how to find common elements in two array-list. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

Below is a demonstration of the same −

Suppose our input is

First list: [Java, Scala, Shell, JavaScript]
Second list: [Java, Python, Shell]

The desired output would be

The common elements from the two lists are: [Java, Shell]

Algorithm

Step 1 - START
Step 2 - Declare two arrayList namely input_list_1 and input_list_1
Step 3 - Define the values.
Step 4 - Use the in-built function .retainAll() to get all the common elements from both the lists.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.ArrayList;
public class Demo {
   public static void main(String[] args){
      ArrayList<String>
      input_list_1 = new ArrayList<String>();
      input_list_1.add("Java");
      input_list_1.add("Scala");
      input_list_1.add("Shell");
      input_list_1.add("JavaScript");
      System.out.println("The first list is defined as: " + input_list_1);
      ArrayList<String> input_list_2 = new ArrayList<String>();
      input_list_2.add("Java");
      input_list_2.add("Python");
      input_list_2.add("Shell");
      System.out.println("The second list is defined as: " + input_list_2);
      input_list_1.retainAll(input_list_2);
      System.out.println("\nThe common elements from the two lists are: " + input_list_1);
   }
}

Output

The first list is defined as: [Java, Scala, Shell, JavaScript]
The second list is defined as: [Java, Python, Shell]

The common elements from the two lists are: [Java, Shell]

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.util.ArrayList;
public class Demo {
   static void get_common_elements(ArrayList<String> input_list_1, ArrayList<String>              input_list_2){
      input_list_1.retainAll(input_list_2);
      System.out.println("\nThe common elements from the two lists are: " + input_list_1);
   }
   public static void main(String[] args){
      ArrayList<String> input_list_1 = new ArrayList<String>();
      input_list_1.add("Java");
      input_list_1.add("Scala");
      input_list_1.add("Shell");
      input_list_1.add("JavaScript");
      System.out.println("The first list is defined as: " + input_list_1);
      ArrayList<String> input_list_2 = new ArrayList<String>();
      input_list_2.add("Java");
      input_list_2.add("Python");
      input_list_2.add("Shell");
      System.out.println("The second list is defined as: " + input_list_2);
      get_common_elements(input_list_1, input_list_2);
   }
}

Output

The first list is defined as: [Java, Scala, Shell, JavaScript]
The second list is defined as: [Java, Python, Shell]

The common elements from the two lists are: [Java, Shell]

Updated on: 29-Mar-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements