I have two lists, a List<User> localUsers and a List<RemoteUser> remoteUsers. One list are local users on a server, and the other list contains users from another server obtained through gRPC. They might be new, exist within our system and need updating, or are inactive (they exist within the first list but not the second).
In order to determine which users are which, I'd like to perform set operations between these lists, but the lists are between objects of different types. To get around this, I found an implementation of set operations using streams, but they are somewhat cumbersome and difficult to read:
// This operation returns a difference between remote users and local users
// signifies new users that don't yet exist on the server
List<RemoteUser> newUsers = remoteUsers.stream()
.filter(remoteUser ->
localUsers.stream().noneMatch(
localUser ->
localUser.getUserName().equals(checkAddPrefix(remoteUser.getLogin())))).collect(Collectors.toList());
I realize that I can do away with the ambiguity of the stream by just doing some kind of double for loop, but anyway...
Finding the inactive users is the same as above with the lists switched around.
Common users between both groups can be performed with
remoteUsers.removeAll(newRemoteUsers)
Is there a cleaner way to perform set operations between Lists of differing object types? Time complexity of the difference operation is O(m * n) for m = len(localusers), n = len(remoteUsers).
Can I get better than this?