When working with a stream of strings, we may need to concatenate or append them into a single string. This Java 8 tutorial explores various techniques for joining or appending a stream of strings using a delimiter with examples using Java 8 Stream API and utilities such as String.join and Collectors.
1. Joining with Delimiter, Prefix and Suffix using Collectors.joining()
Java Collectors class has the following 3 overloaded static methods for string join operations.
- joining() – the input elements are concatenated into a String, in encounter order.
- joining(delimiter) – the input elements are concatenated into a String, separated by the specified delimiter, in encounter order.
- joining(delimiter, prefix, suffix) – the input elements are concatenated into a String, separated by the specified delimiter, with the specified prefix and suffix, in encounter order.
Stream<String> stream = Stream.of("Hello", "World", "Java", "Streams");
// Joining with no delimiter
String result = stream.collect(Collectors.joining());
System.out.println("Joined String: " + result); // Output: HelloWorldJavaStreams
// Joining with specified delimiter
String result = stream.collect(Collectors.joining(", "));
System.out.println("Joined String: " + result); // Output: Hello, World, Java, Streams
// Joining with specified delimiter, prefix and suffix
String result = stream.collect(Collectors.joining(", ", "[", "]"));
System.out.println("Joined String: " + result); // Output: [Hello, World, Java, Streams]
The program output:
HelloWorldJavaStreams
Hello, World, Java, Streams
[Hello, World, Java, Streams]
Streams may contain null values, which can cause issues during concatenation. Use filter() to remove nulls before joining.
Stream<String> stringStream = Stream.of("Hello", null, "Java", "Streams");
String result = stringStream.filter(element -> element != null)
.collect(Collectors.joining(", "));
System.out.println("Joined String: " + result); // Output: Hello, Java, Streams
2. Simple Joining with Delimiter using String.join()
The String.join() is another simple method for concatenating strings. It works well when converting a collection or array of strings to a single string.
The String.join() requires a collection or array as input, so we must convert the stream into a list using toList().
For example, if we use a comma as a delimiter, then this method will result in a comma-separated string.
import java.util.stream.Stream;
Stream<String> stringStream = Stream.of("Hello", "World", "Java", "Streams");
// Joining with a delimiter
String result = String.join(", ", stringStream.toList());
System.out.println("Joined String: " + result); // Output: Hello, World, Java, Streams
Program output.
Hello, World, Java, Streams
3. Append Stream Elements using StringBuilder
For large streams, using StringBuilder is more efficient than concatenating strings directly. We can append each element of the stream to a StringBuilder.
Stream<String> stream = Stream.of("Hello", "World", "Java", "Streams");
StringBuilder result = stream.collect(
() -> new StringBuilder(),
(sb, str) -> sb.append(sb.length() > 0 ? "," : "").append(str),
StringBuilder::append
);
System.out.println("Joined String: " + result);
The program output:
Hello,World,Java,Streams
4. Conclusion
As discussed above, various methods can be used to join or append a stream of strings in Java. In most use cases, Collectors.joining() is the preferred choice due to its simple syntax and efficient performance.
Method | Use Case |
---|---|
Collectors.joining() | The most concise way to join the stream elements with delimiter, prefix and suffix |
String.join() | Simple concatenation with a delimiter |
StringBuilder | The efficient way to apply custom logic during appending |
Drop me your questions related to string joining with Java Streams.
Happy Learning !!
Comments