Skip to content
Advertisement

Java: convert List to a join()d String

JavaScript has Array.join()

JavaScript

Does Java have anything like this? I know I can cobble something up myself with StringBuilder:

JavaScript

.. but there’s no point in doing this if something like it is already part of the JDK.

Advertisement

Answer

String.join

With Java 8 you can do this without any third party library.

If you want to join a Collection of Strings you can use the String.join() method:

JavaScript

Collectors.joining

If you have a Collection with another type than String you can use the Stream API with the joining Collector:

JavaScript

The StringJoiner class may also be useful.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement