To be specific, I have two lists:
List<SystemUserWithNameAndId> list1;
List<SystemUserWithNameAndId> list2;
I want to check if they contain the same system users, and ordering is not a problem. I tried to use a comparator to sort them first, and then check if they are equal using the equals () method of the lists. But I don’t want to redefine the equals method for SystemUserWithNameAndId, and I was wondering if I could use a matchr created for sorting or similar, to check for equality without explicitly repeating lists after sorting.
Comparator<SystemUserWithNameAndId> systemUserComparator = new Comparator<SystemUserWithNameAndId>()
{
@Override
public int compare(SystemUserWithNameAndId systemUser1, SystemUserWithNameAndId systemUser2)
{
final int systemUserId1 = systemUser1.getSystemUserId();
final int systemUserId2 = systemUser2.getSystemUserId();
return systemUserId1 == systemUserId2
? 0
: systemUserId1 - systemUserId2;
}
};
Collections.sort(systemUsers1, systemUserComparator);
Collections.sort(systemUsers2, systemUserComparator);
return systemUsers1.equals(systemUsers2);
Ideally, I want to say
CollectionUtils.isEqualCollections(systemUsers1, systemUsers2, someCustomComparator);
source
share