Joining a List in Java with commas and “and”

后端 未结 8 2042
梦谈多话
梦谈多话 2020-12-01 11:48

Given a list

List l = new ArrayList();
l.add(\"one\");
l.add(\"two\");
l.add(\"three\");

I have a method

<
8条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 12:14

    With Java 8, you can use streams with joiners.

    Collection strings;
    ...
    String commaDelimited = strings.stream().collect(Collectors.joining(","));
    // use strings.parallelStream() instead, if you think
    //   there are gains to be had by doing fork/join
    

提交回复
热议问题