Why does undefined Generics types of Collections d

2019-07-16 12:46发布

问题:

class Test<G extends String>{
    public G test(){return null;}
    public List<G> tests(){return new ArrayList<>();}
}
public void doTest(Test t){
    //works fine
    String str = t.test();
    //Compile error: expected String found Object
    str = t.tests().iterator().next();
}

I would like the last line to return a String instance instead of Object, as the type G was bound to subclass String. There is any other way than casting ?

回答1:

No, the type as been erased. You're running into two different rules for type erasure - from JLS section 4.6:

  • The erasure of a parameterized type (§4.5) G is |G|.
  • The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

So the erasure of List<G> is List, but the erasure of G is String - that's why the first assignment works.

All you need to do to get this code to compile is use a wildcard in your parameter:

public void doTest(Test<?> t)