Explain the syntax of Collections.emptyList()

后端 未结 5 1850
故里飘歌
故里飘歌 2020-12-24 01:35

I just studied about generic programming, the List interface, and ArrayList, so I can understand the statement below.

Arra         


        
5条回答
  •  Happy的楠姐
    2020-12-24 02:01

    Firstly, Collections is a helper library of static methods that operate on various types of collections. If you've done any sort of C++ programming, it's very synonymous to the library of functions.

    In this case, you're invoking the static method emptyList(), which returns an empty list of a particular type. Since these methods still require a type, but Collections' methods can refer to many types, you have to specify the type upon invocation.

    In order to call a generic method, you must call it with the parameter list (i.e. ) before the method name, but after the dot.

    List someList = Collections.emptyList();
    

提交回复
热议问题