Collectors joining() method in Java
In this post, we’ll talk about the Collectors joining() method in Java.
1. Using Collectors.joining() method
The Collectors.joining() method returns a Collector that concatenates the input elements into a single string.
⮚ Convert a primitive character array to a string
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { // Convert a primitive character array to a string char[] charArray = { 'J', 'a', 'v', 'a' }; String string = Stream.of(charArray) // Stream<char[]> .map(arr -> new String(arr)) // Stream<String> .collect(Collectors.joining()); System.out.println(string); // Java } } |
⮚ Convert a list of Character to a string
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { // Convert a list of `Character` to a string List<Character> chars = Arrays.asList('J', 'a', 'v', 'a'); String string = chars.stream() // Stream<Character> .map(String::valueOf) // Stream<String> .collect(Collectors.joining()); System.out.println(string); // Java } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Convert a list of `Character` to a string List<Character> chars = Arrays.asList('J', 'a', 'v', 'a'); StringBuilder sb = new StringBuilder(); for (Character character : chars) { sb.append(character); } String string = sb.toString(); System.out.println(string); // Java } } |
2. Using Collectors.joining(delimiter) method
Collectors.joining(delimiter) returns a Collector that concatenates the input elements, separated by the specified delimiter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("RED", "BLUE", "BLACK", "GREEN"); // Convert elements to strings and concatenate them, separated by commas String joined = colors.stream().collect(Collectors.joining(", ")); System.out.println(joined); } } |
Output:
RED, BLUE, BLACK, GREEN
3. Using Collectors.joining(delimiter, prefix, suffix) method
Collectors.joining(delimiter, prefix, suffix) returns a Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("RED", "BLUE", "BLACK", "GREEN"); String joined = colors.stream().collect(Collectors.joining(", ", "{", "}")); System.out.println(joined); } } |
Output:
{RED, BLUE, BLACK, GREEN}
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; import java.util.StringJoiner; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("RED", "BLUE", "BLACK", "GREEN"); StringJoiner joiner = new StringJoiner(", ", "{", "}"); for (String color : colors) { joiner.add(color); } String joined = joiner.toString(); System.out.println(joined); } } |
Output:
{RED, BLUE, BLACK, GREEN}
That’s all about the Collectors class joining() method in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)