Collectors joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) example
Description
Collectors joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
returns a Collector that concatenates the elements, separated by the delimiter, with the specified prefix and suffix, in encounter order.
Syntax
joining
has the following syntax.
public static Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Example
The following example shows how to use joining
.
import java.util.stream.Collectors;
import java.util.stream.Stream;
//from w w w .ja va2 s.c om
public class Main {
public static void main(String[] args) {
Stream<String> s = Stream.of("a","b","c");
String names = s
.collect(Collectors.joining(", ", "Hello ", ". Goodbye."));
System.out.println(names);
}
}
The code above generates the following result.