Thursday, January 27, 2022

Java Stream - Collectors.joining() With Examples

If you want to join stream elements into a String you can do that using joining() method of the java.util.stream.Collectors class. It is a handy utility method to concatenate elements of an array, collection into a String.

Syntax of Collectors.joining() method

There are three overloaded Collectors.joining() method

  • Collector<CharSequence,?,String> joining()- Returns a Collector that concatenates the input elements into a String, in encounter order.
  • Collector<CharSequence,?,String> joining(CharSequence delimiter)- Returns a Collector that concatenates the input elements, separated by the specified delimiter, in encounter order.
  • Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)- Returns a Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in encounter order.

Collectors.joining() Java examples

1. We have an array of Strings and we want to join all the array elements to get a String. That can be quickly done using Collectors.joining() method by using the array as a Stream source and then calling the collect method. We'll also use the other two variants of joining() method in this example.

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

public class CollectorsJoining {
  public static void main(String[] args) {
    String[] strArr = { "Example", "to", "demonstrate", "Collectors", "joining"};
      String str1 = Stream.of(strArr).collect(Collectors.joining());
      System.out.println("Concatenated String- " + str1);
      
      // Second Variant- Passing Space as delimiter
      String str2 = Stream.of(strArr).collect(Collectors.joining(" "));
      System.out.println("Concatenated String with delimiter- " + str2);
      // Passing "-" as delimiter
      str2 = Stream.of(strArr).collect(Collectors.joining("-"));
      System.out.println("Concatenated String with delimiter- " + str2);
      
      // Third Variant- 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- ExampletodemonstrateCollectorsjoining
Concatenated String with delimiter- Example to demonstrate Collectors joining
Concatenated String with delimiter- Example-to-demonstrate-Collectors-joining
Concatenated String with delimiter and suffix, prefix- [Example|to|demonstrate|Collectors|joining]

2. Using Collectors.joining method to concatenate elements of an ArrayList.

public class CollectorsJoining {
  public static void main(String[] args) {
    List<String> alphabetList = Arrays.asList("A", "B", "C", "D","E", "F");
      String str1 = alphabetList.stream().collect(Collectors.joining());
      System.out.println("Concatenated String- " + str1);
      
      // Second Variant- Passing Space as delimiter
      String str2 = alphabetList.stream().collect(Collectors.joining(" "));
      System.out.println("Concatenated String with delimiter- " + str2);
      // Passing "-" as delimiter
      str2 = alphabetList.stream().collect(Collectors.joining("-"));
      System.out.println("Concatenated String with delimiter- " + str2);
      
      // Third Variant- Passing delimiter, suffix and prefix
      String str3 = alphabetList.stream().collect(Collectors.joining("|", "[", "]"));
      System.out.println("Concatenated String with delimiter and suffix, prefix- " + str3);
  }
}

Output

Concatenated String- ABCDEF
Concatenated String with delimiter- A B C D E F
Concatenated String with delimiter- A-B-C-D-E-F
Concatenated String with delimiter and suffix, prefix- [A|B|C|D|E|F]

That's all for this topic Java Stream - Collectors.joining() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Java Stream - Collectors.partitioningBy() With Examples
  2. Java Stream - findAny() With Examples
  3. Java Stream - noneMatch() With Examples
  4. Java Stream API Interview Questions And Answers

You may also like-

  1. Externalizable Interface in Java
  2. Lock Striping in Java Concurrency
  3. Switch Expressions in Java 12
  4. Java String Interview Questions And Answers
  5. Find Maximum And Minimum Numbers in a Given Matrix Java Program
  6. BeanPostProcessor in Spring Framework
  7. How to Install Node.js and NPM in Windows
  8. How to Write a Map Only Job in Hadoop MapReduce

No comments:

Post a Comment