java-8

How to execute an Array of CompletableFuture and compose their results

那年仲夏 提交于 2020-12-31 04:41:58
问题 I am investigating Java 8 CompletableFutures and read (and seen) that I should employ thenCompose instead of thenApply . I have converted my code to use thenCompose but I have a feeling in an incorrect manner. Here is my controlling code... final CompletableFuture<List<String>> extractor = get(htmlPageSource); @SuppressWarnings("unchecked") final CompletableFuture<List<Documentable>>[] completableFutures = new CompletableFuture[ENDPOINT.EXTRACTABLES.size()]; int index = 0; for( ENDPOINT

How to execute an Array of CompletableFuture and compose their results

一笑奈何 提交于 2020-12-31 04:40:39
问题 I am investigating Java 8 CompletableFutures and read (and seen) that I should employ thenCompose instead of thenApply . I have converted my code to use thenCompose but I have a feeling in an incorrect manner. Here is my controlling code... final CompletableFuture<List<String>> extractor = get(htmlPageSource); @SuppressWarnings("unchecked") final CompletableFuture<List<Documentable>>[] completableFutures = new CompletableFuture[ENDPOINT.EXTRACTABLES.size()]; int index = 0; for( ENDPOINT

How to execute an Array of CompletableFuture and compose their results

狂风中的少年 提交于 2020-12-31 04:40:00
问题 I am investigating Java 8 CompletableFutures and read (and seen) that I should employ thenCompose instead of thenApply . I have converted my code to use thenCompose but I have a feeling in an incorrect manner. Here is my controlling code... final CompletableFuture<List<String>> extractor = get(htmlPageSource); @SuppressWarnings("unchecked") final CompletableFuture<List<Documentable>>[] completableFutures = new CompletableFuture[ENDPOINT.EXTRACTABLES.size()]; int index = 0; for( ENDPOINT

MapStruct add a new calculated field to the dto

ぐ巨炮叔叔 提交于 2020-12-30 08:25:29
问题 I'm trying to map an entity Order to a OrderDTO using MapStruct. I want to add to OrderDTO a new field total , this field is not available in the original entity Order and should be calculated using the information available in the Order (order entries price, quantity, taxes...). I created a new field total in the OrderDTO and I'm trying to map it by adding a default method to the mapper interface: public interface OrderMapper { ... default BigDecimal orderToTotal(Order order){ return

How to implement the Elvis operator in Java 8?

百般思念 提交于 2020-12-30 05:16:51
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional

How to implement the Elvis operator in Java 8?

可紊 提交于 2020-12-30 05:14:08
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional

How to implement the Elvis operator in Java 8?

对着背影说爱祢 提交于 2020-12-30 05:14:03
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional

Java 8 Strem filter map in map — Map<String,Map<String,Employee>>

你。 提交于 2020-12-29 15:01:27
问题 How to filter a Map<String,Map<String,Employee>> using Java 8 Filter? I have to filter only when any of employee in the list having a field value Gender = "M". Input: Map<String,Map<String,Employee>> Output: Map<String,Map<String,Employee>> Filter criteria: Employee.genter = "M" Also i have to return empty map if the filtered result is empty. I tried the below, but it is not working as expected. It is returning the only if all the Employees are with gender "M". tempCollection.entrySet()

Flattening a list of elements in Java 8 Optional pipeline

做~自己de王妃 提交于 2020-12-29 13:19:54
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

Flattening a list of elements in Java 8 Optional pipeline

你说的曾经没有我的故事 提交于 2020-12-29 13:19:12
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to