Why is Collection not simply treated as Collection<?>

后端 未结 4 1637
猫巷女王i
猫巷女王i 2021-01-01 09:03

Consider the following API method taken from Shiro\'s org.apache.shiro.subject.PrincipalCollection interface but probably present in other libraries as well:



        
4条回答
  •  轮回少年
    2021-01-01 09:29

    A use-case that I can think of as to why Collection is not considered as Collection is let say we have a instance of ArrayList

    Now if the instance is of type ArrayList or ArrayList or ArrayList, you can add that type only(type checking). ArrayList is not equivalent to ArrayList.

    But with only ArrayList, you can add object of any type. This may be one of the reason why compiler is not considering ArrayList as ArrayList (type checking).

    One more reason could be backward compatibility with Java version that didn't have generics.

    提交回复
    热议问题