【问题标题】:Can be replaced with method reference可以用方法参考代替
【发布时间】:2017-12-15 11:35:53
【问题描述】:

我有这样的代码,但编译器建议用方法参考替换accounts != null 部分。我该怎么做? 也许还有其他可能性来检查

mMainApi.accounts(user, password)
            .filter(accounts -> accounts != null)
            .map(/* Something here */)
            .subscribe();

我找到了一种使用 .filter(Objects::nonNull) 的解决方案,但该解决方案需要 Android API > 24,所以这实际上没有帮助。

【问题讨论】:

    标签: android lambda


    【解决方案1】:

    你可以试试下面的代码

    mMainApi.accounts(user, password)
                .filter(accounts ->{ 
                  return accounts!=null;
                })
                .map(/* Something here */)
                .subscribe();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2018-10-06
      • 2021-11-16
      • 2013-06-05
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多