Why collection literals?

前端 未结 11 874
无人及你
无人及你 2020-12-29 04:09

From the various online articles on Java 7 I have come to know that Java 7 will be having collection literals1 like the following:

List

        
11条回答
  •  佛祖请我去吃肉
    2020-12-29 05:09

    I think one of the reasons, is that of( ... ) will generate an unchecked warning if you would try to populate it with instances of generic objects.

    The reason, ... expands to a java array, and java arrays do not like generic instances.

    Here is an example from the spec that deals with this specific issue:

    List> pascalsTriangle =
        [[1],
         [1, 1],
         [1, 2, 1],
         [1, 3, 3, 1],
         [1, 4, 6, 4, 1]]
    

    There is currently no way to initialize this variable in this concise manner and without unchecked warning.

提交回复
热议问题