You could transform the lists to lists of ids and then compare them:
return list1.stream().map(MyClass::getId).collect(Collectors.toList())
.equals(list2.stream().map(MyClass::getId).collect(Collectors.toList()));
This question is very similar to this question, so you can take more ideas from there. In my answer you can see that in one of the options I proposed is creating a reusable utility method for lists comparison:
<T, U> boolean equal(List<T> list1, List<T> list2, Function<T, U> mapper) {
List<U> first = list1.stream().map(mapper).collect(Collectors.toList());
List<U> second = list2.stream().map(mapper).collect(Collectors.toList());
return first.equals(second);
}
Then all you need to write is:
return equal(list1, list2, MyClass::getId);