Stream().map() result as Collectors.toMap() value

Weeedooo :

I'm having List<InstanceWrapper>, for each element I want to do some logic that will result in some String message. Then, I want to take create Map<String, String>, where key is InstanceWrapper:ID and value is message;

private String handle(InstanceWrapper instance, String status) {
    return "logic result...";
}

private Map<String, String> handleInstances(List<InstanceWrapper> instances, String status) {
    return instances.stream().map(instance -> handle(instance, status))
                    .collect(Collectors.toMap(InstanceWrapper::getID, msg -> msg));     
}

But it wont compile, I'm getting, how do I put stream().map() result into collectors.toMap() value?

The method collect(Collector<? super String,A,R>) in the type Stream<String> is not applicable for the arguments (Collector<InstanceWrapper,capture#5-of ?,Map<String,Object>>)
Andronicus :

You cannot map before collecting to map, because then you're getting Stream of Strings and loosing information about InstanceWrapper. Stream#toMap takes two lambdas - one generating keys and second - generating values. It should be like that:

instances.stream()
    .collect(Collectors.toMap(InstanceWrapper::getID, instance -> handle(instance, status));

The first lambda generates keys: InstanceWrapper::getID, the second one - associated values: instance -> handle(instance, status).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308141&siteId=1