How to use Collectors.joining() in Java 8

44 total views, 1 views today

Welcome to Java 8 series. In this tutorial, you will learn about how to use Collectors.joining() in Java 8.

If you would like to learn about Java 8 Stream operations.Please check out this link.

Collectors.joining() is used in Java 8 streams to join the elements of the Stream into a single value. It concatenates all elements in the stream to form a string value.

There are there different variants of Collectors.joining() operation.

  • Collectors.joining()
  • Collectors.joining() with delimiter
  • Collectors.joining() with delimiter,prefix and suffix

Let us get started in learning each variation.

Collectors.joining()

It allows us to simply join or concatenate all elements into a single string. Based on the official documentation, returns a collector that concatenates the input elements into a string, in encounter order.

List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

String joinedData = fruits.stream().collect(Collectors.joining());

System.out.println(joinedData);

 

Output

applemangopomobananaorangeapple

Collectors.joining() with delimiter

It allows us to simply join or concatenate all elements into a single string but delimited by the given value. Each element will be delimited or separated by the given character sequence.

List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

String joinedData = fruits.stream().collect(Collectors.joining("--"));

System.out.println(joinedData);

 

Output

apple–mango–pomo–banana–orange–apple

 

Collectors.joining() with delimiter,prefix and suffix

It allows us to simply join or concatenate all elements into a single string but delimited by the given value, prefixed and suffixed by the given character. Each element will be delimited or separated by the given character sequence. The first element will be prefixed by the given character sequence and the last element will be suffixed by the given character sequence.

List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

String joinedData = fruits.stream().collect(Collectors.joining(" ","-","*"));

System.out.println(joinedData);

 

Output

-apple mango pomo banana orange apple*

References : Collectors.joining() , Collectors.joining() with delimiter, Collectors.joining() with delimiter,prefix and suffix

 

I hope you like this tutorial. Thanks for reading and Please leave us a comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

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.