我有一个对象列表说car.我想基于使用Java 8的一些参数来过滤此列表.但是如果参数是null,它会抛出NullPointerException.如何过滤掉空值?
目前的代码如下
requiredCars = cars.stream().filter(c -> c.getName().startsWith("M"));
Run Code Online (Sandbox Code Playgroud)
NullPointerException如果getName()返回则抛出null.
我是Java map和filters8中的新用户.我目前正在使用Spark ML库来进行一些ML算法.我有以下代码:
// return a list of `Points`.
List<Points> points = getPoints();
List<LabeledPoint> labeledPoints = points.stream()
.map(point -> getLabeledPoint(point))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果数据正确,则函数getLabeledPoint(Point point)返回new,LabeledPoint否则返回null.我怎样才能过滤(删除)null LabeledPoint对象map?