I have two 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 can this logic be written using a stream instead of a for loop?
IntStreamto generate the indices e.g.IntStream.range(0, 5), then usemap,mapToObjorforEachas appropriate and accessobj1.get(i)andobj2.get(i).IntStream.range(0, a.length).mapToObj(i -> a[i].getName() == b[i].getName()).collect(Collectors.toList());something like this?