A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5324 below:

[java] Issue with type inference of nested lambdas · Issue #5324 · pmd/pmd · GitHub

In the following UnusedPrivateMethod triggers, because type inference infers the wrong type for the result of Stream.map. So reduceBooksAndLenderStatusByLender doesn't apply because of argument type mismatch and is flagged as unused. This is a problem with core type resolution and is not specific to that rule.

Complete Maven project: https://github.com/lolo101/pmd-issue

package org.example.unusedPrivateMethod;

import static java.util.Collections.emptySet;

import java.util.*;
import java.util.stream.*;

public class Main {

    public static void main(String[] args) {
        Library library = new Library(emptySet());
        Map<String, Map<String, String>> map = new Main().run(library);
        System.out.println(map);
    }

    private Map<
        String,
        Map<String, String>
        > run(Library library) {
        return library
            .books()
            .stream()
            .map(
                book ->
                    book
                        .lenders()
                        .stream()
                        .collect(
                            Collectors.toMap(
                                Lender::name,
                                lender ->
                                    Map.of(
                                        book.title(),
                                        lender.status()
                                    )
                            )
                        )
            )
            .reduce(this::reduceBooksAndLenderStatusByLender)
            .orElse(null);
    }

    private Map<
        String,
        Map<String, String>
        > reduceBooksAndLenderStatusByLender(
        Map<String, Map<String, String>> previousMap,
        Map<String, Map<String, String>> nextMap
    ) {
        previousMap.putAll(nextMap);
        return previousMap;
    }
}

record Library(Collection<Book> books) {}
record Book(String title, Collection<Lender> lenders) {}
record Lender(String name, String status) {}

Originally posted by @lolo101 in #5184 (comment)


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4