What is the Collectors.joining() method in Java?

What is the Collectors class?

Collectors is a utility class that provides various implementations of reduction operations, including grouping elements, collecting them to different collections, summarizing them according to various criteria, etc. The different functionalities in the Collectors class are usually used as the final operation on streams.

The joining() method

joining() is a static method of Collectors that returns a Collector that concatenates the input elements with the specified delimiter. There are three variations of the joining() method:

  1. joining()
  2. joining(CharSequence delimiter)
  3. joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

The joining method is defined in the Collectors class. The Collectors class is defined in the java.util.stream package. To import the Collectors class, use the following import statement:

import java.util.stream.Collectors;

joining()

This method concatenates the input elements into a string in the encountered order.

Syntax


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

Parameters

This method has no arguments.

Return value

The joining() method returns the concatenated string.

Code

Let’s have a look at the code below:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
// List of strings
List<String> stringList = Arrays.asList("educative", "io", "edpresso");
// print the contents of the list
System.out.println("Stream before modification - " + stringList);
// create a stream from the list
Stream<String> stringStream = stringList.stream();
// concat the elements of the list using the joining method
String concatenatedString = stringStream.map(String::toUpperCase).collect(Collectors.joining());
// result after the concatenation
System.out.println("Resulting string after joining - " + concatenatedString);
}
}

joining(CharSequence delimiter)

This method concatenates the input elements, separated by the specified delimiter, into a string in the encountered order.

Syntax


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

Parameters

  • CharSequence delimiter: The delimiter to use.

Return value

This method returns the concatenated string.

Code

Let’s have a look at the code below:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
// List of strings
List<String> stringList = Arrays.asList("educative", "io", "edpresso");
// print the contents of the list
System.out.println("Stream before modification - " + stringList);
// create a stream from the list
Stream<String> stringStream = stringList.stream();
// delimiter to use
String delimiter = "-";
// concat the elements of the list using the joining method
String concatenatedString = stringStream.map(String::toUpperCase).collect(Collectors.joining(delimiter));
// result after the concatenation
System.out.println("Resulting string after joining - " + concatenatedString);
}
}

joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

This method concatenates the input elements with the specified prefix and suffix into a string in the encountered order. The input elements are already separated by the given delimiter.

Syntax


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

Parameters

This method has no arguments.

Return value

This method returns the concatenated string.

Code

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
// List of strings
List<String> stringList = Arrays.asList("educative", "io", "edpresso");
// print the contents of the list
System.out.println("Stream before modification - " + stringList);
// create a stream from the list
Stream<String> stringStream = stringList.stream();
// delimiter to use
String delimiter = "-";
// prefix to use
String prefix = "prefix-";
// suffix to use
String suffix = "-suffix";
// concat the elements of the list using the joining method
String concatenatedString = stringStream.map(String::toUpperCase).collect(Collectors.joining(delimiter, prefix, suffix));
// result after the concatenation
System.out.println("Resulting string after joining - " + concatenatedString);
}
}