Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List class wrapped by Map class do not support generic types in JAVA?

class definition

public interface ITest {
    Long foo();
}

public class Test implements ITest{

    @Override
    public Long foo() {
        return 0L;
    }
}

methods

    public Map<Long, List<? extends ITest>> moo() {
        List<Test> a = List.of(new Test());
        Map<Long, List<? extends ITest>> b = Map.of(1L, a);
        return b;
    }

    public Map<Long, List<? extends ITest>> koo() {
        List<Test> a = List.of(new Test());
        Map<Long, List<Test>> b = Map.of(1L, a);
        return b;
    }

    public List<? extends ITest> too() {
        List<Test> a = List.of(new Test());
        return a;
    }

I got an error only at koo() function. Why this could not work? List class that wrapped by Map class do not support generic?
The below is what I met.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Incompatible types. Found: 'java.util.Map<java.lang.Long,java.util.List<Test>>', required: 'java.util.Map<java.lang.Long,java.util.List<? extends ITest>>'

I think it works properly, but it didn’t.

>Solution :

Your too method compiles because a List<Test> is a subtype of the declared return type of the method, List<? extends ITest>. Your explicit ? extends in the return type makes List<Test> a subtype.

Your koo method does not compile because even though List<Test> is a subtype of List<? extends ITest>, a Map<Long, List<Test>> is not a subtype of Map<Long, List<? extends ITest>>. Nesting that generics relationship in another generic type is not allowed, because you’re using invariant generics.

If you removed ? extends from the return type of too, then that method wouldn’t compile because List<Test> is not a subtype of List<ITest>.

Like what is needed for too, add ? extends to the return type for koo to get this to compile. The type Map<Long, List<Test>> is a subtype of Map<Long, ? extends List<? extends ITest>> because of this added ? extends.

//               vvvvvvvvv
public Map<Long, ? extends List<? extends ITest>> koo() {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading