VOOZH about

URL: https://www.javacodegeeks.com/2014/03/java-can-finally-join-strings.html

⇱ Java can finally join strings


I am sure you were in a situation in which you wanted to join multiple strings. If you were using a programming language other than Java you probably used the join() function provided by the programming language. If you were using Java you could not do this. There was no join() method. The Java Standard Class Library provided you tools for building GUI applications, accessing databases, sending stuff over the network, doing XML transformations or calling remote methods. A simple method for joining a collection of strings was not included. For this you needed one of various third party libraries.

Luckily this time is over now! In Java 8 we finally can join Strings!
 
 
Java 8 added a new class called StringJoiner. As the name suggests we can use this class to join strings:

StringJoiner joiner = new StringJoiner(",");
joiner.add("foo");
joiner.add("bar");
joiner.add("baz");
String joined = joiner.toString(); // "foo,bar,baz"

// add() calls can be chained
joined = new StringJoiner("-")
 .add("foo")
 .add("bar")
 .add("baz")
 .toString(); // "foo-bar-baz"

StringJoiner is used internally by the two new static join() methods of String:

// join(CharSequence delimiter, CharSequence... elements)
String joined = String.join("/", "2014", "10", "28" ); // "2014/10/28"

// join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
List<String> list = Arrays.asList("foo", "bar", "baz");
joined = String.join(";", list); // "foo;bar;baz"

There is also a joining Collector available for the new Stream API:

List<Person> list = Arrays.asList(
 new Person("John", "Smith"),
 new Person("Anna", "Martinez"),
 new Person("Paul", "Watson ")
);

String joinedFirstNames = list.stream()
 .map(Person::getFirstName)
 .collect(Collectors.joining(", ")); // "John, Anna, Paul"

So we do no longer need third party libraries for joining strings!

Reference: Java can finally join strings from our JCG partner Michael Scharhag at the mscharhag, Programming and Stuff blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Java 8
πŸ‘ Photo of Michael Scharhag
Michael Scharhag
March 26th, 2014Last Updated: March 26th, 2014
3 143 1 minute read

Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
bawse
12 years ago

Why not use something like StringBuilder?

0
Reply
Shridharan
12 years ago
Reply to  bawse

Hi Bawse,

I think the difference is this. In case of StringBuilder or StringBuffer you have to add delimited element after every add() method. But this StringJoiner eliminates that need.

0
Reply
Tom
7 years ago

I consider the whole world of freely available classes to make up β€œthe Java runtime”. And especially with the modularized JRE since Java 9 a lot of classes require a module to be added anyhow. So if there are third party libraries doing this perfectly fine… Why? Why included it in the default JRE? Why spend time on something that already exists?

0
Reply
Back to top button
Close
wpDiscuz