假设以下是带有字符串元素的列表:List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL","NBA", "NFL");
现在,创建一个以特定字母结尾的流和过滤器元素:Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));
现在,将Objects::nonnull用作非null值:List list = stream.filter(Objects::nonNull).collect(Collectors.toList());
以下是在Java中过滤非空值的示例:
示例import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
public static void main(String[] args) {
List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL","NBA", "NFL");
Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));
List list = stream.filter(Objects::nonNull).collect(Collectors.toList());
System.out.println("League names ending with L = "+list);
}
}
输出结果League names ending with L = [BBL, IPL, FPL, NFL]