string.join java 8

h‮‬ttps://www.lautturi.com
string.join java 8

To join a list of strings into a single string in Java 8, you can use the join method of the java.util.StringJoiner class. This method allows you to specify a delimiter string and use it to join the elements of the list into a single string.

Here's an example of how to use the join method to join a list of strings:

List<String> list = Arrays.asList("apple", "banana", "cherry");
String s = String.join(", ", list);  // s is "apple, banana, cherry"

In this example, we're using the ", " string as the delimiter, so the output string will contain the elements of the list separated by commas and spaces. You can use a different delimiter string by specifying it as the first argument to the join method.

You can also use the join method to join an array of strings:

String[] array = {"apple", "banana", "cherry"};
String s = String.join(", ", array);  // s is "apple, banana, cherry"

The join method is part of the Java 8 java.util package, so you will need to import this package in order to use it.

You can find more information about the join method and examples of how to use it in the Java documentation.

Created Time:2017-10-17 20:18:55  Author:lautturi