java-8

Concatenate Optional Lists

心不动则不痛 提交于 2020-11-28 10:45:42
问题 I have three Optional> which have to be combined and returned. I tried to use Optional.map() and flatmap() but was not successful. public Optional<List<Entiy>> getRecords() { Optional<List<Entiy>> entity1 = repo.findAllByStatus("1"); Optional<List<Entiy>> entity2 = repo.findAllByStatus("2"); Optional<List<Entiy>> entity3 = repo.findAllByStatus("3"); //Need to return a concatenation of entity1, 2 and 3 } Any thoughts on how to do is efficiently? 回答1: Something like : return Optional.of(Stream

Concatenate Optional Lists

我是研究僧i 提交于 2020-11-28 10:45:07
问题 I have three Optional> which have to be combined and returned. I tried to use Optional.map() and flatmap() but was not successful. public Optional<List<Entiy>> getRecords() { Optional<List<Entiy>> entity1 = repo.findAllByStatus("1"); Optional<List<Entiy>> entity2 = repo.findAllByStatus("2"); Optional<List<Entiy>> entity3 = repo.findAllByStatus("3"); //Need to return a concatenation of entity1, 2 and 3 } Any thoughts on how to do is efficiently? 回答1: Something like : return Optional.of(Stream

Why lambda inside map is not running?

*爱你&永不变心* 提交于 2020-11-28 08:57:47
问题 I am trying to learn concurrency and lambdas in java 8. But my code is not entering lambda block inside map. List<Book> bookList = new ArrayList<Book>(); isbnList .stream() .map(isbn -> (CompletableFuture.supplyAsync( () -> { try { List<String> pageContents = getUrlContents(webLink + isbn); return new Book( parseBookTitle(pageContents), isbn, parseRank(pageContents) ); } catch (IOException ex) { return null; } })).thenApply(a -> bookList.add(a)) ); While debugging, code is exiting at .map

Why lambda inside map is not running?

≡放荡痞女 提交于 2020-11-28 08:57:25
问题 I am trying to learn concurrency and lambdas in java 8. But my code is not entering lambda block inside map. List<Book> bookList = new ArrayList<Book>(); isbnList .stream() .map(isbn -> (CompletableFuture.supplyAsync( () -> { try { List<String> pageContents = getUrlContents(webLink + isbn); return new Book( parseBookTitle(pageContents), isbn, parseRank(pageContents) ); } catch (IOException ex) { return null; } })).thenApply(a -> bookList.add(a)) ); While debugging, code is exiting at .map

Filter null values after map in Java 8 [duplicate]

偶尔善良 提交于 2020-11-28 04:41:54
问题 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: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

DateTimeParseException: Text could not be parsed at index 2 [duplicate]

三世轮回 提交于 2020-11-28 03:31:26
问题 This question already has answers here : java DateTimeFormatterBuilder fails on testtime (2 answers) Java - Unparseable date (2 answers) Closed 2 years ago . I am really confused now why the following snippet results in DateTimeParseException. public static void main(String[] args) { java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz"); String date = "Mon, 10 Sep 2018 23:57:09 UTC"; System.out.println(dtf.parse(date)); } The

DateTimeParseException: Text could not be parsed at index 2 [duplicate]

陌路散爱 提交于 2020-11-28 03:30:12
问题 This question already has answers here : java DateTimeFormatterBuilder fails on testtime (2 answers) Java - Unparseable date (2 answers) Closed 2 years ago . I am really confused now why the following snippet results in DateTimeParseException. public static void main(String[] args) { java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz"); String date = "Mon, 10 Sep 2018 23:57:09 UTC"; System.out.println(dtf.parse(date)); } The

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 =