
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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]
- Related Articles
- Java Program to Find Common Elements Between Two Arrays
- Python Program to Find Common Elements in Two Arrays
- Java program to find common elements in three sorted arrays
- Java Program to Remove duplicate elements from ArrayList
- Golang Program to find the common elements from two arrays
- C++ Program to find the common elements from two arrays
- JavaScript Program for find common elements in two sorted arrays
- Swift Program to Find Common Array Elements
- Golang Program To Find Common Array Elements
- Python program to find common array elements
- Java Program to append all elements of other Collection to ArrayList
- C# program to find common elements in three sorted arrays
- Python program to find common elements in three sorted arrays?
- Sort Elements in an ArrayList in Java
- Java Program to Empty an ArrayList in Java

Advertisements