我在Java 8中使用map
和filters
的新用户。我目前正在使用Spark ML
库来处理某些ML算法。
我有以下代码:
// return a list of `Points`.
List<Points> points = getPoints();
List<LabeledPoint> labeledPoints = points.stream()
.map(point -> getLabeledPoint(point))
.collect(Collectors.toList());
如果数据正确,函数getLabeledPoint(Point point)
将返回新的LabeledPoint
,否则返回null。如何在null
之后过滤(删除)LabeledPoint
map
个对象?
答案 0 :(得分:14)
Stream上有filter
方法:
// return a list of `Points`.
List<Points> points = getPoints();
List<LabeledPoint> labeledPoints = points.stream()
.map(point -> getLabeledPoint(point))
// NOTE the following:
.filter(e -> e != null)
.collect(Collectors.toList());