I have an entity UserId that has both a name and an email. I need to return a map where the UserId can be mapped to either the name or email. Currently this is how it looks where we are able to obtain only the email.
Map<UserId, String> userIdToEmailMap = futureList.stream()
.map(CompletableFuture::join)
.filter(Optional::isPresent)
.map(userProfile -> userProfile.get())
.collect(Collectors.toMap(UserProfile::getUserId, UserProfile::getEmail));
And I need even the name to be retrieved. Is there a way I can do that without having to create another separate map and having to return a list of Maps?
This is wrong but something like this -
Map<UserId, String> userIdToEmailMap = futureList.stream()
.map(CompletableFuture::join)
.filter(Optional::isPresent)
.map(userProfile -> userProfile.get())
.collect(Collectors.toMap(UserProfile::getUserId, UserProfile::getEmail, UserProfile::getName));
UserProfileitself:Collectors.toMap(UserProfile::getUserId, u -> u)