I have a list of User where I want to convert it into a Map of Id and User. However, the list can be null, so if I use Stream to convert it will throw exception.
public Map<Long, User> getUserMap() {
List<User> users = getUserList(); //the method getUserList() can
// return null and this method can not be further changed
return users.stream()
.collect(Collectors.toMap(User::getId, user - > user)));
//throwing nullPointerException when users is null
}
Instead of returning an empty Map, I consider to change the return type as Optional. In the following, it seems to me that if I already use Optional maybe the whole operation can be chained up into one-liner similar to the Stream operation, rather doing the if else null checking. Is this possible ?
public Optional<Map<Long, User>> getUserMap() {
List<User> users = getUserList(); //the method getUserList() can return null
if (users == null) {
return Optional.empty();
} else {
return Optional.of(users.stream()
.collect(Collectors.toMap(User::getId, user - > user)));
}
}
//not working
public Optional<Map<Long, User>> getUserMap() {
List<User> users = getUserList(); //the method getUserList() can return null
return Optional.ofNullable(users)
.stream()
.collect(Collectors.toMap(User::getId, user -> user));
}