Java 8 Streams | Collectors.joining() method with Examples

The joining() method of Collectors Class, in Java, is used to join various elements of a character or string array into a single string object. This method uses stream to do so. There are various overloads of joining method present in the Collector class.

Class Heirarchy:

java.lang.Object
  ↳ java.util.stream.Collectors

joining()

java.util.stream.Collectors.joining() is the most simple joining method which do not takes any parameter. It returns a Collector which joins or concatenates the input streams into String in the order of there appearance.



Syntax:

public static Collector<CharSequence, ?, String> joining()

Below are the illustration for how to use joining() method:

Program 1: Using joining() with an array of characters:

In the below program, a character array is created in ‘ch’. Then this array is fed to be converted into Stream using Stream.of(). Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character array is joined into a String using Collectors.joining() method. It is stored in ‘chString’ variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character array
        char[] ch = { 'G', 'e', 'e', 'k', 's',
                      'f', 'o', 'r',
                      'G', 'e', 'e', 'k', 's' };
  
        // Convert the character array into String
        // using Collectors.joining() method
        String chString = Stream.of(ch)
                              .map(arr -> new String(arr))
                              .collect(Collectors.joining());
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:

GeeksforGeeks

Program 2: Using joining() with a list of characters:

In the below program, a character list is created in ‘ch’. Then this list is fed to be converted into Stream using ch.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character list is joined into a String using Collectors.joining() method. It is stored in ‘chString’ variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character list
        List<Character> ch = Arrays.asList('G', 'e', 'e', 'k', 's',
                                           'f', 'o', 'r',
                                           'G', 'e', 'e', 'k', 's');
  
        // Convert the character list into String
        // using Collectors.joining() method
        String chString = ch.stream()
                              .map(String::valueOf)
                              .collect(Collectors.joining());
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:

GeeksforGeeks

Program 3: Using joining() with n list of string:

In the below program, a String list is created in ‘str’. Then this list is fed to be converted into Stream using str.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character list is joined into a String using Collectors.joining() method. It is stored in ‘chString’ variable.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character list
        List<String> str = Arrays.asList("Geeks", "for", "Geeks");
  
        // Convert the character list into String
        // using Collectors.joining() method
        String chString = str.stream()
                              .map(String::valueOf)
                              .collect(Collectors.joining());
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:

GeeksforGeeks

joining(delimiter)

java.util.stream.Collectors.joining(CharSequence delimiter) is an overload of joining() method which takes delimiter as parameter, of the type CharSequence. A delimiter is a symbol or a CharSequence that are used to seperate words from each other. For example, in every sentence, space ‘ ‘ is used as the by default delimiter for the words in it. It returns a Collector which joins or concatenates the input elements into String in the order of there appearance, seperated by the delimiter.

Syntax:

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)

Below are the illustration for how to use joining(delimiter) method:

Program 1: Using joining(delimiter) with a list of characters:

In the below program, a character list is created in ‘ch’. Then this list is fed to be converted into Stream using ch.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character list is joined into a String using Collectors.joining() method with “, ” passed as the delimiter. It is stored in ‘chString’ variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character list
        List<Character> ch = Arrays.asList('G', 'e', 'e', 'k', 's',
                                           'f', 'o', 'r',
                                           'G', 'e', 'e', 'k', 's');
  
        // Convert the character list into String
        // using Collectors.joining() method
        // with, as the delimiter
        String chString = ch.stream()
                              .map(String::valueOf)
                              .collect(Collectors.joining(", "));
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:

G, e, e, k, s, f, o, r, G, e, e, k, s

Program 2: Using joining(delimiter) with a list of string:

In the below program, a String list is created in ‘str’. Then this list is fed to be converted into Stream using str.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character list is joined into a String using Collectors.joining() method with “, ” passed as the delimiter. It is stored in ‘chString’ variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character list
        List<String> str = Arrays.asList("Geeks", "for", "Geeks");
  
        // Convert the character list into String
        // using Collectors.joining() method
        String chString = str.stream()
                              .map(String::valueOf)
                              .collect(Collectors.joining(", "));
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:



Geeks, for, Geeks

joining(delimiter, prefix, suffix)

java.util.stream.Collectors.joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) is an overload of joining() method which takes delimiter, prefix and suffix as parameter, of the type CharSequence.

A delimiter is a symbol or a CharSequence that are used to seperate words from each other. A prefix is a symbol or a CharSequence that is joined at the starting of the 1st element of the String. Then suffix is also a CharSequence parameter but this is joined after the last element of the string. i.e. at the end.

For example, in every {Geeks, for, Geeks}, space ‘ ‘ is used as the by default delimiter for the words in it. The ‘{‘ is the prefix and ‘}’ is the suffix. It returns a Collector which joins or concatenates the input elements into String in the order of there appearance, seperated by the delimiter.

Syntax:

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter. 
                                                       CharSequence prefix,
                                                       CharSequence suffix))

Below are the illustration for how to use joining(delimiter, prefix, suffix) method:

Program 1: Using joining() with a list of characters:

In the below program, a character list is created in ‘ch’. Then this list is fed to be converted into Stream using ch.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character list is joined into a String using Collectors.joining() method with “, ” passed as the delimiter, “[” as the prefix and “]” as the suffix. It is stored in ‘chString’ variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character list
        List<Character> ch = Arrays.asList('G', 'e', 'e', 'k', 's',
                                           'f', 'o', 'r',
                                           'G', 'e', 'e', 'k', 's');
  
        // Convert the character list into String
        // using Collectors.joining() method
        // with, as the delimiter
        String chString = ch.stream()
                              .map(String::valueOf)
                              .collect(Collectors.joining(", ", "[", "]"));
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:

[G, e, e, k, s, f, o, r, G, e, e, k, s]

Program 2: Using joining() with a list of string:

In the below program, a String list is created in ‘str’. Then this list is fed to be converted into Stream using str.stream() method. Then the resulted stream is mapped for a sequential series using map(). At last the sequential stream containing the character list is joined into a String using Collectors.joining() method with “, ” passed as the delimiter, “{” as the prefix and “}” as the suffix. It is stored in ‘chString’ variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the working
// of the Collectors.joining() method
  
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a character list
        List<String> str = Arrays.asList("Geeks", "for", "Geeks");
  
        // Convert the character list into String
        // using Collectors.joining() method
        String chString = str.stream()
                              .map(String::valueOf)
                              .collect(Collectors.joining(", ", "{", "}"));
  
        // Print the concatenated String
        System.out.println(chString);
    }
}

chevron_right


Output:

{Geeks, for, Geeks}

My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.