Java Stream Collectors.joining() Method With Examples

In this tutorial we’ll see how to use Collectors.joining() method to concatenate the input elements into a String. It is a handy utility method provided by the Collectors class in Java Stream API to quickly convert array elements or elements in a collection to String.

There are three overloaded Collectors.joining() method-

  • Collector<CharSequence,?,String> joining()– Concatenates the input elements into a String, in encounter order.
  • Collector<CharSequence,?,String> joining(CharSequence delimiter)– In this method you can also pass a delimiter, it concatenates the input elements, separated by the specified delimiter, in encounter order.
  • Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)– This method concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in encounter order.

Collectors.joining() Java Stream API examples

1. In this example we’ll pass a character array as a stream to the collect method where Collectors.joining() method is used to get a single string concatenating all the characters of the character array.

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JoiningDemo {

  public static void main(String[] args) {
    char[] ch = { 'T', 'h', 'i', 's', ' ',
                  'i', 's', ' ',
                  'S', 't', 'r', 'i', 'n', 'g' };
    String str1 = Stream.of(ch).map(c->new String(c)).collect(Collectors.joining());
    System.out.println("Concatenated String- " + str1);
  }
}

Output

Concatenated String- This is String

2. In this example we’ll pass an array of String as a Stream to the collect method to get a single string. We’ll also use the joining method where delimiter is passed as an argument.

public class JoiningDemo {

  public static void main(String[] args) {
    String[] strArr = { "This", "is", "a", "String" };
    String str1 = Stream.of(strArr).collect(Collectors.joining());
    System.out.println("Concatenated String- " + str1);
    
    // Passing Space as delimiter
    String str2 = Stream.of(strArr).collect(Collectors.joining(" "));
    System.out.println("Concatenated String with delimiter- " + str2);
    // Passing pipe as delimiter
    str2 = Stream.of(strArr).collect(Collectors.joining("|"));
    System.out.println("Concatenated String with delimiter- " + str2);
    
    // Passing delimiter, suffix and prefix
    String str3 = Stream.of(strArr).collect(Collectors.joining("|", "[", "]"));
    System.out.println("Concatenated String with delimiter and suffix, prefix- " + str3);
  }
}

Output

Concatenated String- ThisisaString
Concatenated String with delimiter- This is a String
Concatenated String with delimiter- This|is|a|String
Concatenated String with delimiter and suffix, prefix- [This|is|a|String]

3. In this example we’ll join the elements of an ArrayList using Collectors.joining() method of the Java Stream API.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class JoiningDemo {

  public static void main(String[] args) {
    List<String> cityList = Arrays.asList("Delhi", "Mumbai", "London", "New York","Bengaluru");
    String str1 = cityList.stream().collect(Collectors.joining());
    System.out.println("Concatenated String- " + str1);
    
    // Passing Space as delimiter
    String str2 = cityList.stream().collect(Collectors.joining(" "));
    System.out.println("Concatenated String with delimiter- " + str2);
    // Passing pipe as delimiter
    str2 = cityList.stream().collect(Collectors.joining("|"));
    System.out.println("Concatenated String with delimiter- " + str2);
    
    // Passing delimiter, suffix and prefix
    String str3 = cityList.stream().collect(Collectors.joining("|", "[", "]"));
    System.out.println("Concatenated String with delimiter and suffix, prefix- " + str3);
  }
}

Output

Concatenated String- DelhiMumbaiLondonNew YorkBengaluru
Concatenated String with delimiter- Delhi Mumbai London New York Bengaluru
Concatenated String with delimiter- Delhi|Mumbai|London|New York|Bengaluru
Concatenated String with delimiter and suffix, prefix- [Delhi|Mumbai|London|New York|Bengaluru]

Related Posts

That’s all for the topic Java Stream Collectors.joining() Method With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.