Java – Join Elements of ArrayList<String> with Delimiter
To join elements of given ArrayList<String> arrayList
with a delimiter string delimiter
, use String.join() method. Call String.join() method and pass the delimiter string delimiter
followed by the ArrayList<String> arrayList
.
String.join() returns a single string with all the string elements of ArrayList joined with delimiter in between them.
Java Program
</>
Copy
import java.util.ArrayList;
import java.util.Arrays;
public class Example {
public static void main(String[] args) {
String delimiter = "--";
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList("hello", "world", "welcome"));
String result = String.join(delimiter, arrayList);
System.out.println("Resulting String: " + result);
}
}
Output
Resulting String: hello--world--welcome
Conclusion
In this Java Tutorial, we learned how to join elements of ArrayList<String> with a specified delimiter in Java.