I assume by common Java, you are talking about Java, and not Javascript. However you called it a script... a snippet of code is also a near must to get good answers on stack overflow, I don't know whether you are using arrays, lists, or even Maps... I will give an answer for each. But please, do give more detailed information next time you ask a question.
Lists have a method called contains() which you can pass an object into.
From here there are two ways to work with it, the preferred way is through Lists
package com.gamsion.chris;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SOListExample {
public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
List<String> list3 = new ArrayList<String>();
String name1 = "John";
String name2 = "Chris";
String name3 = "Madalyn";
list1.add(name1);
list2.add(name1);
list2.add(name2);
list3.add(name2);
list3.add(name3);
list1.add(name3);
list2.add(name3);
List<List<String>> lists = new ArrayList<List<String>>();
lists.add(list1);
lists.add(list2);
lists.add(list3);
System.out.println(getCommonFriends(lists));
}
public static List<String> getCommonFriends(List<List<String>> lists) {
List<String> commonfriends = lists.get(0);
Iterator<List<String>> it = lists.iterator();
while(it.hasNext()){
commonfriends.retainAll(it.next());
}
return commonfriends;
}
}
That is the best answer I can give. Of course adapt to your needs and it should fit well. (I just made that as a testable example. The way you get those lists and how many is totally up to you.) It's also the most effective as far as I know, although the initial "commonfriends = lists,get(0)" does annoy me haha.
EDIT: Adding answer for comment below.
To do that, suggest adding another method that just compares two Lists like this:
public static List<String> compareCommonFriends(List<String> list1, List<String> list2){
list1.retainAll(list2);
return list1;
}
Also in case you want to do an if something was found, whether it was the compareCommonFreinds() or the getCommonFriends(), you can do something like this:
if(compareCommonFriends(list1, list2).isEmpty()){
//your code
}
//OR
if(!compareCommonFriends(list1, list2).isEmpty()){
//your code
}