在Java 8或更高版本中:
String listString = String.join(", ", list);
Returns a new String composed of copies of the {@code CharSequence elements} joined together with a copy of the specified {@code delimiter}.
String message = String.join("-", "Java", "is", "cool");
**// message returned is: "Java-is-cool"**
List<String> strings = new LinkedList<>();
strings.add("Java");strings.add("is");
strings.add("cool");
String message = String.join(" ", strings);
**// message returned is: "Java is cool"**
如果list不是String类型,则可以使用collector:
String listString = list.stream().map(Object::toString)
.collect(Collectors.joining(", "));
Returns a {@code Collector} that concatenates the input elements,
separated by the specified delimiter, in encounter order.
请大家批评 指正 ! 谢谢