HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 8 / Java 8 – Join or append stream of strings

Java 8 – Join or append stream of strings

Learn to join stream of strings with a separator/delimiter using Collectors.joining() API in Java 8.

1. Collectors.joining() method

Java Collectors class has following 3 overloaded static methods for string join operations.

  • joining() – the input elements are concatenated into a String, in encounter order.
  • joining(CharSequence delimiter) – the input elements are concatenated into a String, separated by the specified delimiter, in encounter order.
  • joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) – – the input elements are concatenated into a String, separated by the specified delimiter, with the specified prefix and suffix, in encounter order..

Read More : Java 8 StringJoiner Example

2. Join stream of strings – example

Collectors.joining() method takes separator string as argument and join all the strings in the stream using using this separator.

For example, we use comma as separator then this method will result into a comma-separated string.

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

public class Main 
{
	public static void main(String[] args) 
	{
		Stream<String> words = Arrays.asList("A", "B", "C", "D").stream();
		
		String joinedString = words.collect(Collectors.joining());		//ABCD
		
		System.out.println( joinedString );	

		joinedString = words.collect(Collectors.joining(","));		//A,B,C,D
		
		System.out.println( joinedString );

		joinedString = words.collect(Collectors.joining(",", "{", "}"));	//{A,B,C,D}
		
		System.out.println( joinedString );
	}
}

Program output.

ABCD
A,B,C,D
{A,B,C,D}

Drop me your questions related to string joining with java 8 streams.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 Write to File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)