Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused by the following data type

Tags:

java

generics

List<? extends List<? extends ObservationInteger>>

Just to give you a background which probably has nothing to do with the question. Trying to use the JAHMM library to build and score HMM's.

One of the parameters to the functions mentions the above as the datatype and I have no idea what it means.

From what I understand with help from a friend List<? extends ObservationInteger> means a List of instances of any classes extending "ObservationInteger" which is a valid class in the library.

It is the outer List<? extends List<?... that is confusing me.

Can someone throw some light on this?

like image 969
Ashwin Avatar asked Oct 28 '13 04:10

Ashwin


4 Answers

List<? extends List... means that it can be List of any Collections implementing List interface.

List<List<? extends ObservationInteger>> list = new ArrayList<List<ObservationInteger>>(); - compiler error because without ? extends compiler requires exact match:

List<List<? ObservationInteger>> list = new ArrayList<List<? extends ObservationInteger>>(); - OK

but this looks better

List<? extends List<? ObservationInteger>> list = new ArrayList<List<ObservationInteger>>(); - OK

like image 94
Evgeniy Dorofeev Avatar answered Oct 05 '22 22:10

Evgeniy Dorofeev


It means any Class implementing List Interface with instances of any Class implementing List Interface with instances of any classes extending "ObservationInteger"

like image 38
Pratik Tari Avatar answered Oct 05 '22 22:10

Pratik Tari


It is a List of Objects, which are all instances of a class that extends List.Because those objects are instances of Lists, each of them happens to contain a certain amount of Objects, which are all instances of a class that extends ObservationInteger.

like image 23
qwertyuiop5040 Avatar answered Oct 05 '22 22:10

qwertyuiop5040


It's a list of lists of things. Visualize it as a two-dimensional structure (rows & columns).

The ? extends means it is also valid for any subtypes of List and any subtypes of ObservationInteger.

like image 21
Boann Avatar answered Oct 05 '22 22:10

Boann