Java 15: Wrong error "Required type: Object; Provided: <lambda parameter>"

Answered

Hi,

I just switched my Spring Boot project from Java 14 to Java 15. Now I get errors in "stream.forEach()" on "java.util.List". I call a method there that takes an "Object" instance as the parameter. But IntelliJ complains "Required type: Object; Provided: <lambda parameter>". And IntelliJ doesn't show which code inspection that is - it should, optionally.

Anyhow, below's a short screencast. Does anybody know how to fix that?


Regards,
Karsten Silz

0
5 comments

There were other lambda errors with JDK 15: IntelliJ couldn't infer the class of at least some lambda parameters anymore. It then marked the methods I called on these lambda parameters as unknown.

I got fed up and switched back to Java 14, and all of this trouble went away.

0

Hello, 

Can you please provide a little code sample where the issue is reproduced for you (to reproduce on our side). Thank you 

0

@... Sorry, I didn't want to spend any more time on this. I can't share my code because it's proprietary, so I'd have to create a sample project just for this. I didn't.

I hope somebody else reports it so that it'll be fixed by the time I switch to Java 16.

0

Same issue. Using correto and/or OpenOracle Jdk 20 and facing the same issue.

Not sure if this the compiler or my code.

Map<String,String> getResponseHeaders( HttpServletResponse httpResponse ) {
    return httpResponse.getHeaderNames().stream()
            .collect( key -> Collectors.toMap( key, httpResponse.getHeader( key ), HashMap::new ) );

 

“key” comes with the message for 

required type:String
Provided: <lambda parameter>

I also tried using .map(String::toString) to force a string to be provided. I hope it is my code.

 

0

Behaviour of IDEA is correct: trying with 

package org.example;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
       
    }

    Map<String, String> getResponseHeaders(HttpServletResponse httpResponse) {
       return httpResponse.getHeaderNames().stream()
             .collect(key -> Collectors.toMap(key, httpResponse.getHeader(key), HashMap::new));
    }

}

it's not compiled with e.g. j19:

java: no suitable method found for collect((key)->Col[...]:new))
   method java.util.stream.Stream.<R>collect(java.util.function.Supplier<R>,java.util.function.BiConsumer<R,? super java.lang.String>,java.util.function.BiConsumer<R,R>) is not applicable
     (cannot infer type-variable(s) R
       (actual and formal argument lists differ in length))
   method java.util.stream.Stream.<R,A>collect(java.util.stream.Collector<? super java.lang.String,A,R>) is not applicable
     (cannot infer type-variable(s) R,A
       (argument mismatch; java.util.stream.Collector is not a functional interface
         multiple non-overriding abstract methods found in interface java.util.stream.Collector))

You can submit a feature request here to introduce a quick fix for such case.

Pls attach a sample project if it compiles for you with Java.

0

Please sign in to leave a comment.