You were given two arraylists as below and you have to identify the common elements present in them.
ArrayList1: [sleenium99, automation, tutorials, java]
ArrayList2: [arraylist, sleenium99, automation, tutorials, collections]
Output is: [sleenium99, automation, tutorials]
Method 1:
We are going to achieve this task with the help of retainAll method present in the ArrayList.
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 |
public static void main(String[] args) { //First ArrayList (list1) ArrayList<String> list1 = new ArrayList<String>(); list1.add("selenium99"); list1.add("automation"); list1.add("tutorials"); list1.add("java"); //Prints list1 contents System.out.println("list1: " + list1); //Second ArrayList (list2) ArrayList<String> list2 = new ArrayList<String>(); list2.add("arraylist"); list2.add("selenium99"); list2.add("automation"); list2.add("tutorials"); list2.add("collections"); //Prints list2 contents System.out.println("list2: " + list2); list1.retainAll(list2); System.out.println("Common elements are: "+list1); } |
Output:
list1: [selenium99, automation, tutorials, java]
list2: [arraylist, selenium99, automation, tutorials, collections]
Common elements are: [arraylist, selenium99, automation, tutorials, collections]
Above method modifies the array list ‘list1’ so at the end of the test it contains only common elements. If your requirement is not to modify the existing array list, then you can take the help of third array list. Please look at the code below.
Method 2:
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 |
public static void main(String[] args) { //First ArrayList (list1) ArrayList<String> list1 = new ArrayList<String>(); list1.add("selenium99"); list1.add("automation"); list1.add("tutorials"); list1.add("java"); //Prints list1 contents System.out.println("list1: " + list1); //Second ArrayList (list2) ArrayList<String> list2 = new ArrayList<String>(); list2.add("arraylist"); list2.add("selenium99"); list2.add("automation"); list2.add("tutorials"); list2.add("collections"); //Prints list2 contents System.out.println("list2: " + list2); //Third ArrayList (list3) ArrayList<String> list3 = new ArrayList<>(list1); list3.retainAll(list2); System.out.println("Common elements are: "+list3); System.out.println("List1: "+list1); } |
Output:
Common elements are: [selenium99, automation, tutorials]
List1: [selenium99, automation, tutorials, java]
Also, we can achieve it by using streams filter.
Method 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public static void main(String[] args) { //First ArrayList (list1) ArrayList<String> list1 = new ArrayList<String>(); list1.add("selenium99"); list1.add("automation"); list1.add("tutorials"); list1.add("java"); //Second ArrayList (list2) ArrayList<String> list2 = new ArrayList<String>(); list2.add("arraylist"); list2.add("selenium99"); list2.add("automation"); list2.add("tutorials"); list2.add("collections"); System.out.println(list1.stream().filter(list2::contains).collect(Collectors.toList())); } |
Output:
[selenium99, automation, tutorials]
This article is contributed by Durga Sreenu Rompalli. If you like selenium99 and would like contribute articles, please share your articles at techygeeks99@gmail.com
Please feel free to correct any mistake in the above article by commenting below.