Java泛型:无法将<x>转换为<!-?扩展X->

时间:2018-08-03 10:08:44

标签: java generics

使用Java 10.0.2 + 13,Gradle 4.9和Guava 26.0-jre构建以下代码将输出此错误:

错误:

argument mismatch; bad return type in method reference

Iterator<Entry<String,ImmutableMap<Path,String>>>

cannot be converted to

Iterator<Entry<? extends String,? extends ImmutableMap<Path,String>>>

代码:

final Map<String, Map<Path, String>> m = new HashMap<>();

// put entries into m

final ImmutableMap<String, ImmutableMap<Path, String>> im =
    ImmutableMap.<String, ImmutableMap<Path, String>>copyOf(
        m.entrySet().stream().map(
            e -> immutableEntry(
                e.getKey(),
                ImmutableMap.copyOf(e.getValue())
            )
        )::iterator
    )
;

为什么StringImmutableMap<Path,String>分别不满足? extends String? extends ImmutableMap<Path,String>

我如何才能正确编译它?

1 个答案:

答案 0 :(得分:1)

也许这不能直接回答您的问题,但是不是使用... <properties> .... <sonar.exclusions> **/generated/**/*, **/model/**/* </sonar.exclusions> <sonar.test.exclusions> src/test/**/* </sonar.test.exclusions> .... <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath> <sonar.coverage.exclusions> **/generated/**/*, **/model/**/* </sonar.coverage.exclusions> <jacoco.version>0.7.5.201505241946</jacoco.version> .... </properties> .... 吗?

赞:

Collector

EDIT :就像 curosity 一样,这可以使您的代码得以编译(但 please 请使用ImmutableMap<String, ImmutableMap<Path, String>> im = m.entrySet().stream() .collect(ImmutableMap.toImmutableMap( Map.Entry::getKey, e -> ImmutableMap.copyOf(e.getValue()) )); -version ,它比下面的代码更清晰):

Collector