Andy897
Andy897

Reputation: 7133

Bad return type in lamba expression

I am getting the following compilation error with the given code -

Bad return type in lamba expression: Map < String, Set< Param>> can not be converted to Map < String, List< Map< String, Object>>>

I have no clues at all why this error. As mapToReturn and dummyMap are of same type.

    Map<String, Set<Param>> mapToReturn = Optional.ofNullable(CACHE.get(content, (key) -> {
        hit.set(false);
        Map<String, List<Map<String, Object>>> rawMap = Paser.parse(Map.class, key);
        Map<String, Set<Param>>  dummyMap = new HashMap<>();

        for (Map.Entry<String, List<Map<String, Object>>> entry : rawMap.entrySet()) {
            dummyMap.put(entry.getKey(), entry.getValue()
                    .stream()
                    .map(this::mapToParam)
                    .collect(Collectors.toSet()));
        }

        return dummyMap;
    })).orElseThrow(() -> new ParamParserException("... "));

Upvotes: 0

Views: 497

Answers (1)

Lachezar Balev
Lachezar Balev

Reputation: 12031

The signature of CACHE.get certainly matters and this is the reason for which you are getting this error. I tried to reproduce the problem. Consider the relevant to your question part of the code:

Map<String, Set<Param>> aMap = Optional.ofNullable(CACHE.get(content, (key) -> {
            //...
            Map<String, Set<Param>>  dummyMap = new HashMap<>();
            //...
            return dummyMap;
        })).orElseThrow(() -> new ParamParserException("... "));

This code will compile, if the signature of CACHE.get is the following:

static class CACHE 
{
    static Map<String, Set<Param>> get(String content, Function<String, Map<String, Set<Param>>> func)
    {
        return ...;
    }
}

Now let's change the signature:

static class CACHE
{
   static Map<String, Set<Param>> get(String content, Function<String, Map<String, List<Map<String, Object>>>> func)
    {
         return ...;
    }
 }

This fails with the following error:

bad return type in lambda expression Map<String,Set<Param>> cannot be converted to Map<String,List<Map<String,Object>>>

Which is exactly the error which you are getting. The signature of Paser.parse is not the reason because the error message would have been different.

Upvotes: 1

Related Questions