forked from vishal8113/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jav 3
48 lines (37 loc) · 1.09 KB
/
jav 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Java Program to find common elements
// in two ArrayLists
// Using retainAll() method
// import ArrayList package
import 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);
}
}