【问题标题】:How do I turn this expression into a lambda expression?如何将此表达式转换为 lambda 表达式?
【发布时间】:2021-11-18 10:59:31
【问题描述】:

我想把我正在做的事情变成 lambda,在这种情况下,我会在另一个列表中滚动 (listRegistrationTypeWork),检查子列表 (getRegistrationTypeWorkAuthors) 是否为 != null,如果是也就是说,滚动它以查找 authorCoautor = 类型,并增加一个计数,以找出列表中有多少记录具有相同的类型。

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;
}

【问题讨论】:

  • 看看stream()flatMap()filter()count(),例如listRegistrationTypeWork.stream().flatMap(tab -> tab.getRegistrationTypeWorkAuthors().stream()).filter(author->author.getAuthorCoauthor().equals(type)).count()。使用 CollectionUtils.isNotEmpty() 意味着其中一些列表可能为空,因此您需要在此处添加一些检查,例如filter(tab->tab.getRegistrationTypeWorkAuthors() != null)flatMap() 之前。
  • 到目前为止,您的尝试结果如何?你到底卡在哪里了?
  • Lambda 表达式:(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; }

标签: java lambda


【解决方案1】:

虽然您的陈述对转换为 lambda 表达式 的含义不够清楚,但我假设您希望将命令式循环步骤转换为函数式 stream 和 lambda em> 基于一个。

这应该很简单:

  • 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();
}

【讨论】:

    【解决方案2】:

    除了 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;
    

    【讨论】:

    • 嗨@Stephan Hogenboom 这个想法很棒,但我的列表不是可选的,事实证明它不起作用,然后我必须将其设为可选,但感谢您的贡献!
    • 我试图在 Java 8 中编译,然后在谷歌搜索后发现它的 Java 9 功能。这段代码对我有用。谢谢!!
    猜你喜欢
    • 2010-12-04
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2016-02-10
    • 2011-07-18
    相关资源
    最近更新 更多