1

I have an async method with a completeablefuture result:

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

I have a list of dogs:

List<Dog> dogs;

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 )));

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

Non-static method cannot be referenced from a static context

And the second Dog::getName has an error of:

String is not a functional interface

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

riorio
  • 6,500
  • 7
  • 47
  • 100

1 Answers1

2

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

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

You need toMap(Dog::getName, d -> asyncDogLater(d.getName())).

daniu
  • 14,137
  • 4
  • 32
  • 53