如何将此表达式转换为 lambda 表达式?
How do I turn this expression into a lambda expression?
我想将我正在做的事情变成 lambda,在这种情况下,我将滚动浏览另一个列表 (listRegistrationTypeWork),检查子列表 (getRegistrationTypeWorkAuthors) 是否为 != null,如果是,滚动查看 authorCoautor = type,并增加计数,以找出列表中有多少记录具有相同类型。
public int qtyMaximumWorksByAuthorCoauthor(AuthorCoauthor type) {
int count = 0;
for (RegistrationTypeWork tab : listRegistrationTypeWork) {
if (CollectionUtils.isNotEmpty(tab.getRegistrationTypeWorkAuthors())) {
for (RegistrationTypeWorkAuthors author : tab.getRegistrationTypeWorkAuthors()) {
if (author.getAuthorCoauthor().equals(type))
count++;
}
}
}
return count;
}
虽然您的陈述不够清楚转换为 lambda 表达式 的含义,但我假设您希望将命令式循环步骤转换为函数式 stream 和 lambda 基于一个。
这应该很简单,使用:
filter
从您的两个集合中过滤掉不需要的值
flatMap
将所有内部集合展平为单个流,以便您可以将 count
作为单个来源对其进行操作
public int qtyMaximumWorksByAuthorCoauthor(AuthorCoauthor type) {
return listRegistrationTypeWork.stream()
.filter(tab -> tab.getRegistrationTypeWorkAuthors() != null)
.flatMap(tab -> tab.getRegistrationTypeWorkAuthors().stream())
.filter(author -> type.equals(author.getAuthorCoauthor()))
.count();
}
除了 Thomas 的精彩评论外,我想你会想像这样写你的流。
long count = listRegistrationTypeWork.stream()
// to make sure no lists that are actual null are mapped.
// map all RegistrationTypeWork into optionals of lists of RegistrationTypeWorkAuthors
.map(registrationTypeWork -> Optional.ofNullable(registrationTypeWork.getRegistrationTypeWorkAuthors()))
// this removes all empty Optionals from the stream
.flatMap(Optional::stream)
// this turns the stream of lists of RegistrationTypeWorkAuthors into a stream of plain RegistrationTypeWorkAuthors
.flatMap(Collection::stream)
// this filters out RegistrationTypeWorkAuthors which are of a different type
.filter(registrationTypeWorkAuthors -> type.equals(registrationTypeWorkAuthors.getAuthorCoauthor()))
.count();
// count returns a long so you either need to return a long in your method signature or cast the long to an integer.
return (int) count;
我想将我正在做的事情变成 lambda,在这种情况下,我将滚动浏览另一个列表 (listRegistrationTypeWork),检查子列表 (getRegistrationTypeWorkAuthors) 是否为 != null,如果是,滚动查看 authorCoautor = type,并增加计数,以找出列表中有多少记录具有相同类型。
public int qtyMaximumWorksByAuthorCoauthor(AuthorCoauthor type) {
int count = 0;
for (RegistrationTypeWork tab : listRegistrationTypeWork) {
if (CollectionUtils.isNotEmpty(tab.getRegistrationTypeWorkAuthors())) {
for (RegistrationTypeWorkAuthors author : tab.getRegistrationTypeWorkAuthors()) {
if (author.getAuthorCoauthor().equals(type))
count++;
}
}
}
return count;
}
虽然您的陈述不够清楚转换为 lambda 表达式 的含义,但我假设您希望将命令式循环步骤转换为函数式 stream 和 lambda 基于一个。
这应该很简单,使用:
filter
从您的两个集合中过滤掉不需要的值flatMap
将所有内部集合展平为单个流,以便您可以将count
作为单个来源对其进行操作
public int qtyMaximumWorksByAuthorCoauthor(AuthorCoauthor type) {
return listRegistrationTypeWork.stream()
.filter(tab -> tab.getRegistrationTypeWorkAuthors() != null)
.flatMap(tab -> tab.getRegistrationTypeWorkAuthors().stream())
.filter(author -> type.equals(author.getAuthorCoauthor()))
.count();
}
除了 Thomas 的精彩评论外,我想你会想像这样写你的流。
long count = listRegistrationTypeWork.stream()
// to make sure no lists that are actual null are mapped.
// map all RegistrationTypeWork into optionals of lists of RegistrationTypeWorkAuthors
.map(registrationTypeWork -> Optional.ofNullable(registrationTypeWork.getRegistrationTypeWorkAuthors()))
// this removes all empty Optionals from the stream
.flatMap(Optional::stream)
// this turns the stream of lists of RegistrationTypeWorkAuthors into a stream of plain RegistrationTypeWorkAuthors
.flatMap(Collection::stream)
// this filters out RegistrationTypeWorkAuthors which are of a different type
.filter(registrationTypeWorkAuthors -> type.equals(registrationTypeWorkAuthors.getAuthorCoauthor()))
.count();
// count returns a long so you either need to return a long in your method signature or cast the long to an integer.
return (int) count;