The Collectors.joining() method in Java 8 Streams is a terminal operation used to combine stream elements into a single String. It works on streams of CharSequence and supports optional delimiters, prefixes, and suffixes, making it an efficient and beginner-friendly way to format output.
Variants of Collectors.joining()
Java provides three variants of the Collectors.joining() method.
1. joining()
This is the simplest form of the joining() method. It does not take any parameters and concatenates the stream elements directly in the order of appearance.
Syntax
public static Collector<CharSequence, ?, String> joining()
Example 1: Using joining() with a Character Array
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GFG {
public static void main(String[] args)
{
char[] ch = {'G', 'e', 'e', 'k', 's', 'f',
'o', 'r', 'G', 'e', 'e', 'k', 's'};
String chString = Stream.of(ch).map(String::new).collect(Collectors.joining());
System.out.println(chString);
}
}
Output
GeeksforGeeks
Explanation:
- Stream.of(ch) converts the character array into a stream.
- map(String::new) converts the character array elements into a String.
- Collectors.joining() concatenates all stream elements into a single String.
- The resulting concatenated string is stored in chString.
Example 2: Using joining() with a List of Characters
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<Character> ch = Arrays.asList(
'G', 'e', 'e', 'k', 's', 'f',
'o', 'r', 'G', 'e', 'e', 'k', 's');
String chString = ch.stream().map(String::valueOf).collect(Collectors.joining());
System.out.println(chString);
}
}
Output
GeeksforGeeks
Explanation
- ch.stream() converts the character list into a stream.
- map(String::valueOf) converts each Character into its corresponding String.
- Collectors.joining() concatenates all the string elements into one single String.
- The final concatenated string is stored in chString.
Example 3: Using joining() with a List of Strings
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<String> str = Arrays.asList("Geeks", "for", "Geeks");
String result = str.stream().collect(Collectors.joining());
System.out.println(result);
}
}
Output
GeeksforGeeks
Explanation
- str.stream() converts the string list into a stream.
- Collectors.joining() concatenates all stream elements in their encounter order without any delimiter.
2. joining(delimiter)
This overloaded version allows specifying a delimiter that separates each element in the resulting string.
Syntax
public static Collector<CharSequence, ?, String>
joining(CharSequence delimiter)
Example 1: Using joining(delimiter) with Characters
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<Character> ch = Arrays.asList(
'G', 'e', 'e', 'k', 's', 'f',
'o', 'r', 'G', 'e', 'e', 'k', 's');
String chString = ch.stream().map(String::valueOf).collect(Collectors.joining(", "));
System.out.println(chString);
}
}
Output
G, e, e, k, s, f, o, r, G, e, e, k, s
Explanation:
- ch.stream() converts the character list into a stream.
- map(String::valueOf) converts each Character element into a String.
- Collectors.joining(", ") joins all string elements, separating them with ", ".
Example 2: Using joining(delimiter) with Strings
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<String> str = Arrays.asList("Geeks", "for", "Geeks");
String result = str.stream().collect(Collectors.joining(", "));
System.out.println(result);
}
}
Output
Geeks, for, Geeks
Explanation:
- str.stream() converts the string list into a stream.
- Collectors.joining(", ") joins all stream elements while inserting ", " between each string.
3. joining(delimiter, prefix, suffix)
This version allows adding:
- a delimiter between elements
- a prefix at the beginning
- a suffix at the end of the final string
Syntax
public static Collector<CharSequence, ?, String>
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Example 1: Using joining(delimiter, prefix, suffix) with Characters
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<Character> ch = Arrays.asList(
'G', 'e', 'e', 'k', 's', 'f',
'o', 'r', 'G', 'e', 'e', 'k', 's');
String chString = ch.stream().map(String::valueOf).collect(Collectors.joining(", ", "[", "]"));
System.out.println(chString);
}
}
Output
[G, e, e, k, s, f, o, r, G, e, e, k, s]
Explanation:
- ch.stream() converts the character list into a stream.
- map(String::valueOf) converts each Character into its string representation.
- Collectors.joining(", ", "[", "]") joins the elements using ", " as the delimiter, "[" as the prefix, and "]" as the suffix.
Example 2: Using joining(delimiter, prefix, suffix) with Strings
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<String> str = Arrays.asList("Geeks", "for", "Geeks");
String result = str.stream().collect(Collectors.joining(", ", "{", "}"));
System.out.println(result);
}
}
Output
{Geeks, for, Geeks}
Explanation:
- str.stream() converts the string list into a stream.
- Collectors.joining(", ", "{", "}") joins all strings using ", " as the delimiter, "{" as the prefix, and "}" as the suffix.