HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java 8 – Join String Array – Convert Array to String

By Lokesh Gupta | Filed Under: Java 8

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

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

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

2
Leave a Reply

This comment form is under antispam protection
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Nicolas

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.

Vote Up0Vote Down  Reply
3 years ago
Lokesh Gupta

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

Vote Up0Vote Down  Reply
3 years ago

Search Tutorials

Java 8 Tutorial

  • Java 8 – Introduction
  • Java 8 – forEach
  • Java 8 – Stream
  • Java 8 – Boxed Stream
  • Java 8 – Lambda Expression
  • Java 8 – Functional Interface
  • Java 8 – Method References
  • Java 8 – Default Method
  • Java 8 – Optionals
  • Java 8 – 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 – Join Array
  • Java 8 – Base64
  • Java 8 – Exact Arithmetic
  • Java 8 – Comparator
  • Java 8 – Regex as Predicate
  • Java 8 – Join String
  • Java 8 – Difference Between Dates
  • Internal vs. External Iteration
  • Java 8- SecureRandom

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

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 © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz