How to convert a String into an ArrayList?

前端 未结 13 631
囚心锁ツ
囚心锁ツ 2020-11-28 04:32

In my String, I can have an arbitrary number of words which are comma separated. I wanted each word added into an ArrayList. E.g.:

String s = \"a,b,c,d,e,...         


        
13条回答
  •  再見小時候
    2020-11-28 04:49

    Option1:

    List list = Arrays.asList("hello");
    

    Option2:

    List list = new ArrayList(Arrays.asList("hello"));
    

    In my opinion, Option1 is better because

    1. we can reduce the number of ArrayList objects being created from 2 to 1. asList method creates and returns an ArrayList Object.
    2. its performance is much better (but it returns a fixed-size list).

    Please refer to the documentation here

提交回复
热议问题