I have 2 distinct lists: List1
and List2
, and I want to perform an action for each index where the elements have the same getName()
value:
for (int i = 0; i < 5; i++) {
if (list1.get(i).getName().equals(list2.get(i).getName())) {
// TODO
}
}
Is there a way to do this using Java streams? I have tried the logic:
if (List1().stream().anymatch(x -> List2().stream().anymatch(y -> x.getName().equals(y.getName))))) {
// TODO
}
This works, but first object(index) of List1
is compared with every object(index) of List2
.
What I need is first object(index) of List1
to be compared with first of List2
, second index to second and so on.
How to write this if logic using stream instead of for loop.