英文:
Check if null attribute present in Optional and return String Java Stream API
问题
我有以下内容:
class Person
private String firstName;
private String familyName;
// 设置器和获取器
我有以下方法:
public String getFullName(Optional<Person> persons) {
return persons
.map(person -> (person.getFirstName() + " " + person.getFamilyName())).orElse("无效");
}
我只想检查名字或姓氏是否为null
,如果是,则显示“无效”该人。我在考虑添加一个验证方法,但我确定有一种更简单的方法,我想不出来。
英文:
I have the following
class Person
private String firstName;
private String familyName;
// Setters and Getters
And I have the following method
public String getFullName(Optional<Person> persons) {
return persons
.map(person -> (person.getFirstName() + " " + person.getFamilyName())).orElse("Invalid");
}
I just want to check if either first or last name is null
, display "Invalid"
for that person. I was thinking to add a method for validation but I am sure there is an easier way I cannot think about.
答案1
得分: 4
你正在查看 [Optional::filter
][1],在 map 之前:
return persons
.filter(person -> person.getFamilyName() != null && person.getFirstName() != null)
.map(person -> person.getFirstName() + " " + person.getFamilyName())
.orElse("无效");
这意味着,如果姓氏和名字不为 null,则创建连接;否则返回“无效”消息,或者您甚至可以使用 `orElseThrow` 抛出异常。
[1]: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#filter-java.util.function.Predicate-
英文:
You are looking to [Optional::filter
][1], before the map:
return persons
.filter(person -> person.getFamilyName() != null && person.getFirstName() != null)
.map(person -> person.getFirstName() + " " + person.getFamilyName())
.orElse("Invalid");
Which mean, if the family and first names are not null then create your concatination, otherwise return an Invalid message, or you can even throw an exception by using orElseThrow
[1]: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#filter-java.util.function.Predicate-
答案2
得分: 1
你可以使用filter来实现:
public String getFullName(Optional<Person> persons) {
return persons
.filter(person -> Objects.nonNull(person.getFirstName()) && Objects.nonNull(person.getFamilyName()))
.map(person -> (person.getFirstName() + " " + person.getFamilyName())).orElse("Invalid");
}
英文:
You can use filter for that:
public String getFullName(Optional<Person> persons) {
return persons
.filter(person -> Objects.nonNull(person.getFirstName()) && Objects.nonNull(person.getFamilyName()))
.map(person -> (person.getFirstName() + " " + person.getFamilyName())).orElse("Invalid");
}
答案3
得分: 1
另一种函数式方法。
首先,为了进行必填字段验证,创建一个谓词构建器方法:
public static <T, F> Predicate<T> hasRequiredValue(Function<T, F> fieldGetter) {
return fieldGetter.andThen(Objects::nonNull)::apply;
}
然后稍微修改 getFullName
方法:
public Optional<String> getFullName(Person person) {
return Optional.ofNullable(person)
.filter(hasRequiredValue(Person::getFamilyName))
.filter(hasRequiredValue(Person::getFirstName))
.map(p -> p.getFirstName() + " " + p.getFamilyName());
}
然后按以下方式使用:
Person person = ...
String fullName = getFullName(person).orElse("Invalid");
英文:
Another functional approach.
First create a predicate builder method for required field validation:
public static <T, F> Predicate<T> hasRequiredValue(Function<T, F> fieldGetter) {
return fieldGetter.andThen(Objects::nonNull)::apply;
}
And modify the getFullName
a little bit:
public Optional<String> getFullName(Person person) {
return Optional.ofNullable(person)
.filter(hasRequiredValue(Person::getFamilyName))
.filter(hasRequiredValue(Person::getFirstName))
.map(p -> p.getFirstName() + " " + p.getFamilyName());
}
Then use it as follows:
Person person = ...
String fullName = getFullName(person).orElse("Invalid");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论