HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 8 / Java 8 – Join String Array – Convert Array to String

Java 8 – Join String Array – Convert Array to String

Java examples to join string array to produce single String. This code can be used to convert array to string in Java. We may need this information many times during development specially while parsing contents out of JSON or XML.

1. Join String Array – Java 8 String.join()

String.join() method has two overloaded forms.

Join multiple string arguments

This method takes all strings in var-args format and all strings are passed as argument in the method. The return string is received by appending all strings delimited by argument separator.

String join(CharSequence delimiter, CharSequence... elements)

This method can be used to join multiple strings which are not yet in form of collection or array.

String joinedString = String.join(", ", "How", "To", "Do", "In", "Java");
System.out.println(joinedString);

Output:

How, To, Do, In, Java

Join array or list of strings

String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

This method is used to join array of strings or list of strings.

Java program to join list of strings

List<String> strList = Arrays.asList("How", "To", "Do", "In", "Java");
		
String joinedString = String.join(", ", strList);

System.out.println(joinedString);

Output:

How, To, Do, In, Java

Java program to join array of strings

String[] strArray = { "How", "To", "Do", "In", "Java" };
		
String joinedString = String.join(", ", strArray);

System.out.println(joinedString);

Output:

How, To, Do, In, Java

2. Java 8 StringJoiner for formatted output

Using StringJoiner class, we can produce formatted output of joined string. This is specially useful while using lambda collectors.

2.1. Method Syntax

It’s constructor takes three arguments – delimiter [mandatory], and optionally prefix and suffix.

StringJoiner(CharSequence delimiter)
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

2.2. StringJoiner Example

Run the example with similar input as above example to join multiple strings. We want to format the output as [How, To, Do, In, Java], then we can use below code:

StringJoiner joiner = new StringJoiner(", ", "[", "]");

joiner.add("How")
		.add("To")
		.add("Do")
		.add("In")
		.add("Java");

Output:

[How, To, Do, In, Java]

3. String list of string with Collectors.joining()

While using Java 8 lambda, we can use Collectors.joining() to convert list to String.

List<String> numbers = Arrays.asList("How", "To", "Do", "In", "Java");

String joinedString = 	numbers
						.stream()
						.collect(Collectors.joining(", ","[","]"));

System.out.println(joinedString);

Output:

[How, To, Do, In, Java]

4. Join String Array using StringUtils.join()

The StringUtils class of the Commons Langs library has several join() methods that can be used to combine an array or list of strings into a single string.

4.1. Maven Dependency

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

4.2. StringUtils.join() Example

See given examples. First we are joining string array with empty string. In second example, we are joining array with comma.

String[] strArray = { "How", "To", "Do", "In", "Java" };

String joinedString = StringUtils.join(strArray);
System.out.println(joinedString);

String joinedString2 = StringUtils.join(strArray, ", ");
System.out.println(joinedString2);

Output:

HowToDoInJava
How, To, Do, In, Java

Use above given examples to concatenate string array with in Java.

Happy Learning !!

Reference(s):

Java 8 String Doc
StringJoiner Java Doc
Apache Commons StringUtils Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

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.

Feedback, Discussion and Comments

  1. Nicolas

    April 4, 2016

    Hello! I saw in the example 2 the following:

    String joinedString = numbers
    .stream()
    .map(i -> i.toString())…..

    In this case I think that the map(..) is unnecessary because the collection is already a string.

    • Lokesh Gupta

      April 4, 2016

      Bang !! You are right. I will update the example.

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)