Search code examples
javamodelmapper

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


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?


Solution

  • 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.