如果在Java 8中stream之前collection为null,如何返回false?

uma*_*uma 1 java collections java-8 java-stream

我写了下面的代码来检查某些情况。

/**
 * Returns true if any of the dose detail is an x
 * @return boolean
 */
public <DD extends BD<DI>, DI extends BI> boolean Y(final Collection<DD> dds) {
    return dds.stream().anyMatch(dd -> dd.M().K());
}
Run Code Online (Sandbox Code Playgroud)

但是此方法有一些风险dds,为空。我需要返回false也是dd也为null。如何使用Java 8将此方法修改为null安全?

the*_*ddy 6

或者您可以这样做。更多或类似的方式

return dds != null && dds.stream().anyMatch(dd -> dd.M().K());
Run Code Online (Sandbox Code Playgroud)