如何打开List< Item>到Map< Item,completeablefuture< xyz> [英] How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

查看:80
本文介绍了如何打开List< Item>到Map< Item,completeablefuture< xyz>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步方法,具有一个completeablefuture结果:

I have an async method with a completeablefuture result:

public CompletableFuture<DogLater> asyncDogLater(String dogName){}

我有一条狗清单:

List<Dog> dogs;

现在,我想创建一条从狗的名字到Completeablefuture的地图:

Now, I want to create a map from the dog's name to the Completeablefuture:

Map<String, CompletableFuture<DogLater>> map;

检查 我正在尝试这样做:

After checking this and this I was trying to do so:

    Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream()
            .collect( Collectors.toMap(Dog::getName,
                                       asyncDogLater(Dog::getName )));

但是编译器抱怨第一个 Dog :: getName 有问题,因为:

But the compiler complains that the first Dog::getName is problematic since:


不能从静态上下文中引用非静态方法

Non-static method cannot be referenced from a static context

第二个 Dog :: getName 的错误为:


字符串不是功能接口

String is not a functional interface

我还检查了这篇文章,但是我仍然不确定如何解决这个问题。

I also checked this post, but I'm still not sure how to solve this.

推荐答案

Collectors.toMap()的第二个参数的类型必须为 Function< T,R> ,在您的情况下是 Function< Dog,CompletableFuture< DogLater>>

Collectors.toMap()s second argument needs to be of type Function<T,R>, in your case Function<Dog,CompletableFuture<DogLater>>.

asyncDogLater(Dog :: getName)的类型为 Function< Function< Dog,String>,CompletableFuture< DogLater> > 如果我没记错的话。

asyncDogLater(Dog::getName) is of type Function<Function<Dog, String>, CompletableFuture<DogLater>> if I'm not mistaken.

您需要 toMap(Dog :: getName,d-> asyncDogLater(d.getName()))

这篇关于如何打开List&lt; Item&gt;到Map&lt; Item,completeablefuture&lt; xyz&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆