List> vs List<? extends Map>

前端 未结 5 677
谎友^
谎友^ 2020-12-04 05:45

Is there any difference between

List>

and

List>
<         


        
5条回答
  •  感情败类
    2020-12-04 05:55

    As you mentioned, there could be two below versions of defining a List:

    1. List>
    2. List

    2 is very open. It can hold any object type. This may not be useful in case you want to have a map of a given type. In case someone accidentally puts a different type of map, for example, Map. Your consumer method might break.

    In order to ensure that List can hold objects of a given type, Java generics introduced ? extends. So in #1, the List can hold any object which is derived from Map type. Adding any other type of data would throw an exception.

提交回复
热议问题