假设以下是带有字符串元素的列表:
List<String> leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL","NBA", "NFL");
现在,创建一个以特定字母结尾的流和过滤器元素:
Stream<String> stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));
现在,将Objects::nonnull用作非null值:
List<String> 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<String> leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL","NBA", "NFL"); Stream<String> stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L")); List<String> 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]