java-stream

Filter null values after map in Java 8 [duplicate]

蓝咒 提交于 2020-11-28 04:41:21
问题 This question already has answers here : Filter values only if not null using lambda in Java8 (6 answers) Closed 4 years ago . I'm new in using map and filters in Java 8. I'm currently using Spark ML library for some ML algorithms. I have the following code: // return a list of `Points`. List<Points> points = getPoints(); List<LabeledPoint> labeledPoints = points.stream() .map(point -> getLabeledPoint(point)) .collect(Collectors.toList()); The function getLabeledPoint(Point point) returns a

Filter null values after map in Java 8 [duplicate]

感情迁移 提交于 2020-11-28 04:40:49
问题 This question already has answers here : Filter values only if not null using lambda in Java8 (6 answers) Closed 4 years ago . I'm new in using map and filters in Java 8. I'm currently using Spark ML library for some ML algorithms. I have the following code: // return a list of `Points`. List<Points> points = getPoints(); List<LabeledPoint> labeledPoints = points.stream() .map(point -> getLabeledPoint(point)) .collect(Collectors.toList()); The function getLabeledPoint(Point point) returns a

Difference between map and filter on a java collection stream

ⅰ亾dé卋堺 提交于 2020-11-28 02:41:05
问题 Suppose there is a list say List<String> myList = new ArrayList<String>(); myList.add("okay"); myList.add("omg"); myList.add("kk"); I am doing this: List<String> fianllist = myStream.map(item -> item.toUpperCase()).filter(item ->item.startsWith("O")).collect(Collectors.toList()); My question is what the difference between map and filter as both can take a lambda expression as a parameter. Can some one please explain? 回答1: By using map , you transform the object values. The map operation

Difference between map and filter on a java collection stream

微笑、不失礼 提交于 2020-11-28 02:40:17
问题 Suppose there is a list say List<String> myList = new ArrayList<String>(); myList.add("okay"); myList.add("omg"); myList.add("kk"); I am doing this: List<String> fianllist = myStream.map(item -> item.toUpperCase()).filter(item ->item.startsWith("O")).collect(Collectors.toList()); My question is what the difference between map and filter as both can take a lambda expression as a parameter. Can some one please explain? 回答1: By using map , you transform the object values. The map operation

Averaging across multiple fields with IntSummaryStatistics

▼魔方 西西 提交于 2020-11-28 02:15:24
问题 I'm trying to use Java 8 streams to create a single CarData object, which consists of an average of all the CarData fields in the list coming from getCars ; CarData = new CarData(); CarData.getBodyWeight returns Integer CarData.getShellWeight returns Integer List<CarData> carData = carResults.getCars(); IntSummaryStatistics averageBodyWeight = carData.stream() .mapToInt((x) -> x.getBodyWeight()) .summaryStatistics(); averageBodyWeight.getAverage(); IntSummaryStatistics averageShellWeight =