【发布时间】:2019-06-05 14:37:22
【问题描述】:
我有一个具有可完成未来结果的异步方法:
public CompletableFuture<DogLater> asyncDogLater(String dogName){}
我有一份狗的名单:
List<Dog> dogs;
现在,我想创建一张从狗的名字到 Completeablefuture 的地图:
Map<String, CompletableFuture<DogLater>> map;
Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream()
.collect( Collectors.toMap(Dog::getName,
asyncDogLater(Dog::getName )));
但编译器抱怨第一个Dog::getName 有问题,因为:
不能从静态上下文中引用非静态方法
而第二个Dog::getName有一个错误:
字符串不是函数式接口
我也检查了this post,但我仍然不确定如何解决这个问题。
【问题讨论】:
-
你需要
toMap(Dog::getName, d -> asyncDogLater(d.getName()))。 -
@daniu 完美运行。您想将其添加为荣誉的答案吗?
标签: java java-stream completable-future method-reference