Array to Collection: Optimized code

前端 未结 10 2014
萌比男神i
萌比男神i 2021-01-31 14:26

Is there a better way of achieving this?

public static List toList(String[] array) {

    List list = new ArrayList(array.length);

          


        
10条回答
  •  粉色の甜心
    2021-01-31 14:56

    You can try something like this:

    List list = new ArrayList(Arrays.asList(array));
    

    public ArrayList(Collection c)

    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The ArrayList instance has an initial capacity of 110% the size of the specified collection.

    Taken from here

提交回复
热议问题