✅ Problem :
How do you join a list of strings with a custom delimiter, prefix, and suffix using Java 8 Streams?
✅ Example:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class JoinStringsCustomDelimiter {
public static void main(String[] args) {
List<String> words = Arrays.asList("Apple", "Banana", "Grapes", "Blueberry");
// Join strings with custom delimiter, prefix, and suffix
String result = words.stream()
.collect(Collectors.joining(", ", "[", "]"));
System.out.println(result);
}
}
✅ Output:
[Apple, Banana, Grapes, Blueberry]
✅ Explanation:
- words.stream(): Converts the list of strings into a Stream of String objects.
- Collectors.joining(“, “, “[“, “]”):
", "is the delimiter between elements."["is the prefix added at the beginning of the result."]"is the suffix added at the end of the result.
- The result is a single string with all words joined by the delimiter and wrapped in brackets.
- The output is printed using
System.out.println().