Why I get error 'no instance of type' on map method?

Viewed 81

I have this function:

public <D> List<D> mapList(Object source, List<D> targetClass) {
    return targetClass
            .stream()
            .map(element -> modelMapper.map(source, element))
            .collect(Collectors.toList());
}

On this row:

    map(element -> modelMapper.map(source, element))
    

I have this error:

no instance of type variable r exists so that void conforms to r

Any idea why I get the error above and how to fix it?

2 Answers

map method on Stream class return a Stream.

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

ModelMapper map method returns void.

public void map(Object source, Object destination) {

Therefore the error. "void" cannot be streamed.

Types do not match the as compiler error suggests, the returned type is void and will not be collected . I guess the map method on modelMapper is overloaded and you can pass the class type on second argument as well to resolve the issue. some thing like

Foo foo = modelMapper.map(map, Foo.class);
Related