I have followed this answer to check if two lists of different type are equal based on same fields.
Here the working function :
private <T, U> boolean compareLists(List<T> list1, List<U> list2, BiPredicate<T, U> predicate) {
return list1.size() == list2.size() &&
list1.stream().allMatch(itme1 -> list2.stream().anyMatch(item2 -> predicate.test(itme1, item2)));
}
But when I called it that way
compareLists(list1, list2,
(item1, item2) ->
item1.someField11.equals(item1.someField21) && item1.someField12.equals(item1.someField22)
)
I got Cannot resolve symbol 'someField11'
(same for the other fields)
I had to declare it this way bellow to make it work
BiPredicate<Foo1, Foo2> biPredicate = (item1, item2) ->
item1.someField11.equals(item1.someField21) && item1.someField12.equals(item1.someField22)
So how to make it work without extra local variable biPredicate
?
- UPDATE SOLUTION
list1
was type of Set
not List
so I changed the function signature to Collection