How to convert comma-separated String to List?

前端 未结 24 2320
你的背包
你的背包 2020-11-22 16:58

Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for t

24条回答
  •  温柔的废话
    2020-11-22 17:39

    An example using Collections.

    import java.util.Collections;
     ...
    String commaSeparated = "item1 , item2 , item3";
    ArrayList items = new ArrayList<>();
    Collections.addAll(items, commaSeparated.split("\\s*,\\s*"));
     ...
    

提交回复
热议问题