What is the purpose of List?

前端 未结 4 559
清歌不尽
清歌不尽 2021-02-05 01:01

I didn\'t even know this was doable, but I saw while perusing some code online a method with a signature like this:

public List read( ... )
         


        
4条回答
  •  不知归路
    2021-02-05 01:36

    It is possible that this method signature was created as a by-product of some generic class.

    For example, SwingWorker has two type parameters, one for final result and one for intermediate results. If you just don't want to use any intermediate results, you pass Void as the type parameter, resulting in some methods returning Void - i.e. nothing.

    If there were a method List returnAllIntermediateResults() in SwingWorker with Void as the type parameter V, it would have created a method just like you posted in your question.

    The code would be perfectly valid. You can instantiate any implementation of the List interface (e.g. ArrayList) with type parameter Void. But the only value a Void type can have is null. So the list could not hold anything else but nulls, if the implementation allows null elements.

提交回复
热议问题