A very straightforward but slow approach: (I know there must be faster methods)
Loop through the first list. For each item in that list, loop through the second list to see if there are any item that is the same as the first one.
public static boolean areListsSimilar(ArrayList<String> list1, ArrayList<String> list2) {
for (item1 : list1) {
for (item2 : list2) {
if (item1.equals(item2)) return true;
}
}
return false;
}
Or you can do this (ArrayList
instead of ArrayList<String>
)
public static boolean areListsSimilar(ArrayList list1, ArrayList list2) {
for (item1 : list1) {
for (item2 : list2) {
if (item1.equals(item2)) return true;
}
}
return false;
}
Or add generic constraints to only accepts the arguments that implement Equatable
public static <T> boolean areListsSimilar(ArrayList<? extends Equatable> list1, ArrayList<? extends Equatable> list2) {
for (item1 : list1) {
for (item2 : list2) {
if (item1.equals(item2)) return true;
}
}
return false;
}