
How to Display Duplicate elements from more than one List in java?
//I mean common elements from two List?
Ex: List a1=new ArrayList(); a1.add(50); a1.add(10); a1.add(20); a1.add(30); List a2=new ArrayList(); a2.add(50); a2.add(10); a2.add(20); a2.add(25); a2.add(40);
from above code How to Display common elements Display

Hare is an example that compare two array lists and display the common elements from both of them.
import java.util.*;
public class DisplayCommonElement{
public static void main(String[]args){
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add("a");
a1.add("b");
a1.add("c");
a2.add("a");
a2.add("b");
a2.add("c");
a2.add("d");
a2.add("e");
int array1Size = a1.size();
int array2Size = a2.size();
if (a1.size() > a2.size()) {
int k = 0;
for (int i = 0; i < a2.size(); i++) {
if (((String)a1.get(i)).equals((String)a2.get(i))) {
System.out.println((String)a2.get(i));
}
k = i;
}
}
else {
int k = 0;
for (int i = 0; i < a1.size(); i++) {
if (((String)a1.get(i)).equals((String)a2.get(i))) {
System.out.println((String)a1.get(i));
}
k = i;
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.