3
firstName
및 lastName
이없는 사람을 찾을 수 없을 때 FriendNotFoundException
을 코드에 넣어야합니다. Stream에서 예외를 catch 할 수 있습니까?스트림에서 사용자 정의 예외를 throw
이제는 이와 같은 문제가 있지만 실패합니다.
@Override
public Friend findFriend(String firstName, String lastName) throws FriendNotFoundException {
if (firstName == null || lastName ==null) {
throw new IllegalArgumentException("There are no parameters");
}
if (friends.stream().filter(x -> !firstName.equals(x.getLastName()) &&
(!lastName.equals(x.getLastName()))) != null);
{
throw new FriendNotFoundException(firstName, lastName);
}
return friends.stream().filter(
x -> (firstName.equals(x.getFirstName())) &&
(lastName.equals(x.getLastName()))).findAny().orElse(null);
}
을 그리고 두 조건을 정의하는 것을 생각하지 않는다 코드를 더 읽기 쉽게 만듭니다. 하지만 여분의 괄호를 제거하면 ... – Holger