我刚开始使用Java 8中的map
和filters
。目前我正在使用Spark 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。如何在 map
后过滤(删除)null
的 LabeledPoint
对象?
.filter(Objects::nonNull)
。意思是筛选掉值为 null 的元素。 - Mikhail