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.
joining()
methodjoining()
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:
joining()
joining(CharSequence delimiter)
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.
public static Collector<CharSequence, ?, String> joining()
This method has no arguments.
The joining()
method returns the concatenated string.
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 stringsList<String> stringList = Arrays.asList("educative", "io", "edpresso");// print the contents of the listSystem.out.println("Stream before modification - " + stringList);// create a stream from the listStream<String> stringStream = stringList.stream();// concat the elements of the list using the joining methodString concatenatedString = stringStream.map(String::toUpperCase).collect(Collectors.joining());// result after the concatenationSystem.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.
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)
CharSequence delimiter
: The delimiter to use.This method returns the concatenated string.
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 stringsList<String> stringList = Arrays.asList("educative", "io", "edpresso");// print the contents of the listSystem.out.println("Stream before modification - " + stringList);// create a stream from the listStream<String> stringStream = stringList.stream();// delimiter to useString delimiter = "-";// concat the elements of the list using the joining methodString concatenatedString = stringStream.map(String::toUpperCase).collect(Collectors.joining(delimiter));// result after the concatenationSystem.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.
public static Collector<CharSequence, ?, String> joining()
This method has no arguments.
This method returns the concatenated string.
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 stringsList<String> stringList = Arrays.asList("educative", "io", "edpresso");// print the contents of the listSystem.out.println("Stream before modification - " + stringList);// create a stream from the listStream<String> stringStream = stringList.stream();// delimiter to useString delimiter = "-";// prefix to useString prefix = "prefix-";// suffix to useString suffix = "-suffix";// concat the elements of the list using the joining methodString concatenatedString = stringStream.map(String::toUpperCase).collect(Collectors.joining(delimiter, prefix, suffix));// result after the concatenationSystem.out.println("Resulting string after joining - " + concatenatedString);}}