Comparable VS <? extends Comparable>

梦想的初衷 提交于 2021-02-13 12:14:53

问题


regarding the following code:

public class Test <T extends Comparable>{
    public static void main(String[] args){
        List<String> lst = Array.asList("abc","def");
        System.out.println(func(lst));
    }
    public static boolean func(List<**here**> lst){
        return lst.get(0).compareTo(lst.get(1)) == 0;
    }
}

why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile?

thanks in advance.


回答1:


This happens because generics are invariant. Even if String is a Comparable, meaning:

String s = "";
Comparable c = s; // would work

Generics of these would not work:

List<Comparable> listC = List.of();
List<String> listS = List.of();

listC = listS; // will fail

And this would not work no matter what is the relationship between Comparable and String.

When you change the definition of that method to:

public static boolean func(List<? extends Comparable> lst) {
    ...
}

This is said that: wildcard with an extends-bound makes the type covariant.

This means that :

List<? extends Comparable> listC = List.of();
List<String> listS = List.of();

listC = listS; // would work here

Or in simpler words it means that List<String> is a subtype of List<? extends Comparable>.

There is a small price to pay now, because listC is now a producer of elements, meaning you can take elements out of it, but you can not put anything into it.

And well after you understand this, you are not done yet, because the definition of that method would be entirely correct, when written like this:

 public static <T extends Comparable<? super T>> boolean func(List<T> lst) {
      .....
 }



回答2:


This is because List<SubClass> cannot be cast to List<BaseClass>. Let us suppose that it could

List<String> sList = new ArrayList<>();
List<Comparable> cList = (List<Comparable>) sList;
cList.add(5);

This would be a problem because the integer 5 is not a String and should not be placed in a List<String>.

By using ? extends Comparable you are saying that the function can take a list of anything that has Comparable as a base class (e.g. List<Comparable>, List<String>, List<Integer>, etc.)

To more correctly define your function you should do the following:

public static <T extends Comparable<T>> boolean func(List<T> lst) {}

This enforces that the type is comparable with itself. Your function compares the first and second element in the list, so it is best to ensure that each element in the list is actually comparable with every other element.




回答3:


Because

List<String> lst = Array.asList("abc","def");

lst list has generic type String, not Comparable. String class, hovewer, implement Comparable<String> interface, so it fits in ? extends Comparable generic type.



来源:https://stackoverflow.com/questions/52224806/comparable-vs-extends-comparable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!