I have a requirement to check whether there is a common element in two lists. I came up with two ways to do this:
Method 01 : Loops
private boolean func01 (List<String> list1, List<String> list2) {
for (String group : list1) {
for (String funcGroup : list2) {
if (group.equals(funcGroup)) {
return true;
}
}
}
return false;
}
Method 02 : Lambda
private boolean func02 (List<String> list1, List<String> list2) {
return list1.stream().filter(list2::contains).findAny().isPresent();
}
In my view, I find the first method more readable. What I need to understand is, whether there are any differences or advantages when comparing these two methods?