Java: Convert List to String

后端 未结 5 1485
春和景丽
春和景丽 2020-12-29 01:36

How can I convert List to String? E.g. If my List contains numbers 1 2 and 3 how can it be converted to String = \"1,

5条回答
  •  被撕碎了的回忆
    2020-12-29 01:58

    Just to add another (of many) options from a popular library (Apache Commons):

    import org.apache.commons.lang3.StringUtils;
    
    String joinedList = StringUtils.join(someList, ",");
    

    See documentation: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#join-java.lang.Iterable-java.lang.String-


    An elegant option from others' comments (as of Java 8):

    String joinedList = someList.stream().map(String::valueOf).collect(Collectors.joining(","));
    

提交回复
热议问题