Java generics: cannot convert <X> into <? extends X>

XDR

Building the following code with Java 10.0.2+13, Gradle 4.9 & Guava 26.0-jre outputs this error:

Error:

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

Code:

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
    )
;

Why do String & ImmutableMap<Path,String> not satisfy ? extends String & ? extends ImmutableMap<Path,String>, respectively?

How can I get this to properly compile?

Tomasz Linkowski

Maybe this does not directly answer your question, but is using a Collector not an option?

Like this:

ImmutableMap<String, ImmutableMap<Path, String>> im = m.entrySet().stream()
        .collect(ImmutableMap.toImmutableMap(
                Map.Entry::getKey, e -> ImmutableMap.copyOf(e.getValue())
        ));

EDIT: Just as a curosity, this gets your code to compile (but please use the Collector-version instead, it's much more clear than the code below):

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to convert Milliseconds to "X mins, x seconds" in Java?

From Dev

Issue with using 'extends' and 'super' in java generics with generic methods

From Dev

Java Collections Generics <? extends Employee> throwing exception

From Dev

Java Generics cannot convert Type in Type

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Cannot convert nil to type x

From Dev

Is there a way in Java Generics to say "T extends Thing or T extends SomethingElse"?

From Dev

Java generics: <B extends BaseB> does not match <? extends BaseB>

From Dev

Java Generics error: Cannot convert from E to E?

From Dev

Java Generics E extends Comparable<E> leaves warning

From Dev

Java generics extends syntax

From Dev

Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D>

From Dev

Java Generics - Cannot convert from <? extends MyObject> to <MyObject>

From Dev

Failing to use User object as parameter for ? extends User in Java generics

From Dev

Generics cannot convert type

From Dev

Java Generics - use of extends keyword

From Dev

Cannot cast a class "X" to a class "Y" even though X extends Y?

From Dev

what the usefulness about Java generics involving inheritance and generics extends self

From Dev

super and extends combined for generics in Java

From Dev

PowerShell 5 and classes - Cannot convert the "X" value of type "X" to type "X"

From Dev

How convert [x] into x?

From Dev

cannot convert list to list error in java generics

From Dev

Java generics : Type mismatch: cannot convert from Integer to K

From Dev

java generics T extends Simpletype?

From Dev

Java Collections Generics <? extends Employee> throwing exception

From Dev

Cannot implicity convert type "void" to "x"

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

correct java syntax for generics with extends

From Dev

Java Generics - use of extends keyword

Related Related

  1. 1

    How to convert Milliseconds to "X mins, x seconds" in Java?

  2. 2

    Issue with using 'extends' and 'super' in java generics with generic methods

  3. 3

    Java Collections Generics <? extends Employee> throwing exception

  4. 4

    Java Generics cannot convert Type in Type

  5. 5

    Java generics. Type mismatch: cannot convert from object to

  6. 6

    Cannot convert nil to type x

  7. 7

    Is there a way in Java Generics to say "T extends Thing or T extends SomethingElse"?

  8. 8

    Java generics: <B extends BaseB> does not match <? extends BaseB>

  9. 9

    Java Generics error: Cannot convert from E to E?

  10. 10

    Java Generics E extends Comparable<E> leaves warning

  11. 11

    Java generics extends syntax

  12. 12

    Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D>

  13. 13

    Java Generics - Cannot convert from <? extends MyObject> to <MyObject>

  14. 14

    Failing to use User object as parameter for ? extends User in Java generics

  15. 15

    Generics cannot convert type

  16. 16

    Java Generics - use of extends keyword

  17. 17

    Cannot cast a class "X" to a class "Y" even though X extends Y?

  18. 18

    what the usefulness about Java generics involving inheritance and generics extends self

  19. 19

    super and extends combined for generics in Java

  20. 20

    PowerShell 5 and classes - Cannot convert the "X" value of type "X" to type "X"

  21. 21

    How convert [x] into x?

  22. 22

    cannot convert list to list error in java generics

  23. 23

    Java generics : Type mismatch: cannot convert from Integer to K

  24. 24

    java generics T extends Simpletype?

  25. 25

    Java Collections Generics <? extends Employee> throwing exception

  26. 26

    Cannot implicity convert type "void" to "x"

  27. 27

    Java generics. Type mismatch: cannot convert from object to

  28. 28

    correct java syntax for generics with extends

  29. 29

    Java Generics - use of extends keyword

HotTag

Archive