Taha K&#246;rkem - 1 year ago 292
Java Question

# Intersection of Two Dimensional ArrayList in Java

I want to intersection list1, list2 and list3 in a ArrayList.
So, I want to intersection ArrayLists of arrayList2D. arrayList2D can contain (max) six ArrayList. How I can do?

That's intersection of two Lists.

public <T> List<T> intersection(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();

for (T t : list1) {
if(list2.contains(t)) {
}
}

return list;
}

That's my codes

ArrayList<ArrayList<Integer>> arrayList2D=new ArrayList<ArrayList<Integer>>();

ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
ArrayList<Integer> list3 = new ArrayList<Integer>();

Ok so as per my understanding you need to intersect all arraylists contain under arrayList2D.

public <T> List<T> intersection(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();

for (T t : list1) {
if(list2.contains(t)) {
}
}

return list;
}

make other method which you need to call

public static List<Integer> getAllIntersection(ArrayList<ArrayList<Integer>> arrayList2D){
List<Integer> finalList =arrayList2D.get(0);
for(int i=1;i<arrayList2D.size();i++){

finalList=intersection(finalList,arrayList2D.get(i));
}
System.out.println("firstList"+finalList);
return finalList;
}

then finaly call getAllIntersection(arrayList2D) to get desire result

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download