
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Common Elements in Two ArrayList in Java
In this article, we will learn how to find common elements in two array-list. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arraysthat can grow as needed.
Problem Statement
In the given array, we aim to find the common elements in two ArrayList objects in Java, 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 −
Input −
First list: [Java, Scala, Shell, JavaScript] Second list: [Java, Python, Shell]
Output −
The common elements from the two lists are: [Java, Shell]
Steps to Find Common Elements in Two ArrayList
Below are the steps to find common elements in two ArrayList
- 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]
Code Explanation
The ArrayList class is imported from the java.util package. A public class named Demo is defined. The main method is defined, which is the entry point of the program. An ArrayList named input_list_1 is created to hold String elements. The add method is used to add four programming languages to the list. The contents of the first list are printed. Another ArrayList named input_list_2 is created and three programming languages are added to it. The contents of the second list are printed. The retainAll method is used on input_list_1 to retain only the elements that are also present in input_list_2. This modifies input_list_1 to contain only the common elements. The common elements between the two lists are printed.
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]
Code Explanation
In the given, Java code defines a Demo class with a main method and a static method get_common_elements. The main method creates two ArrayList objects, populates them with some string elements, and then calls the get_common_elements method to find and print the common elements between the two lists.