Java StringJoiner
last modified July 8, 2023
In this article we cover Java StringJoiner.
StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
StringJoiner
is also used internally by the join
method of the String
class.
Using StringJoiner
The following example joins numbers with the StringJoiner
class.
package com.zetcode; import java.util.StringJoiner; public class JoinStringEx { public static void main(String[] args) { var joined = new StringJoiner(","); joined.add("1"); joined.add("2"); joined.add("3"); joined.add("4"); joined.add("5"); System.out.println(joined); } }
The example concatenates five numbers and prints the final string to the console.
var joined = new StringJoiner(",");
A new instance of the StringJoiner
class is created. The comma
character is used as a delimiter.
joined.add("1"); joined.add("2"); joined.add("3"); joined.add("4"); joined.add("5");
Five values are added with the add
method.
System.out.println(join);
The StringJoiner
is converted to a string and printed to the
console.
$ java com/zetcode/JoinStringEx.java 1,2,3,4,5
Java String.join
In the second example, we join strings with the String.join
method.
package com.zetcode; public class JoinStringEx { public static void main(String[] args) { var joined = String.join("/", "2016", "8", "5"); System.out.println(joined); } }
The String.join
method internally uses the StringJoiner
.
var joined = String.join("/", "2016", "8", "5");
A date is concatenated with the String.join
method.
$ java com/zetcode/JoinStringEx.java 2016/8/5
Java join list
The third example concatenates elements of a list.
package com.zetcode; import java.util.List; public class JoinListEx { public static void main(String[] args) { var words = List.of("Today", "is", "a", "beautiful", "day"); var joined = String.join(" ", words); System.out.println(joined); } }
A list can be passed as an argument to the String.join
method.
var joined = String.join(" ", words);
The elements of the list are joined with a single space character.
$ java com/zetcode/JoinListEx.java Today is a beautiful day
Reading CSV file
The following example reads numbers from a CSV file and later joins them with a
StringJoiner
. We use the Gradle build tool to build the example.
build.gradle src └── main ├── java │ └── com │ └── zetcode │ └── JoinStringEx.java └── resources └── numbers.csv
The example has his project structure. The numbers that we are going to read are
located in the resources
directory.
version '1.0' apply plugin: 'java' apply plugin: 'application' sourceCompatibility = 17 mainClassName = "com.zetcode.JoinStringEx"
This is the Gradle build file.
13,24,35,16,50
This is the numbers.csv
file.
package com.zetcode; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.StringJoiner; public class JoinStringEx { public static void main(String[] args) throws FileNotFoundException { var fileName = "src/main/resources/numbers.csv"; var scanner = new Scanner(new File(fileName)); scanner.useDelimiter(","); var joined = new StringJoiner("|"); while (scanner.hasNext()) { joined.add(scanner.next()); } scanner.close(); System.out.println(joined); } }
The example reads CSV file, containing numbers, and joins them with a
StringJoiner
using a different delimiter.
var scanner = new Scanner(new File(fileName)); scanner.useDelimiter(",");
The values are read with the Scanner
class. The numbers are
separated by a comma character so we set the comma delimiter with the
useDelimiter
method.
var joined = new StringJoiner("|");
A StringJoiner
class is instantiated with a "|" delimiter.
while (scanner.hasNext()) { join.add(scanner.next()); }
We retrieve the values with the scanner and concatenate them with the joiner.
$ gradle run -q 13|24|35|16|50
Writing CSV file
The next example writes numbers to a CSV file.
build.gradle src └── main ├── java │ └── com │ └── zetcode │ └── JoinStringEx.java └── resources
The example has his project structure. We are going to create a new file in the current working directory.
version '1.0' apply plugin: 'java' apply plugin: 'application' sourceCompatibility = 17 mainClassName = "com.zetcode.JoinStringEx"
This is the Gradle build file.
package com.zetcode; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.StringJoiner; public class JoinStringEx { public static void main(String[] args) throws IOException { var fileName = "src/main/resources/numbers2.csv"; var joined = new StringJoiner(","); joined.add("21"); joined.add("43"); joined.add("54"); joined.add("76"); joined.add("98"); var newFile = new File(fileName); newFile.createNewFile(); try (var pw = new PrintWriter(newFile)) { pw.write(joined.toString()); } } }
The example joins five numbers with a StringJoiner
and writes the
concatendated string to a CSV file.
var joined = new StringJoiner(","); joined.add("21"); joined.add("43"); joined.add("54"); joined.add("76"); joined.add("98");
Five numbers are concatenated with the StringJoiner
. The numbers
are separated with a comma character.
var newFile = new File(fileName); newFile.createNewFile();
A new file object is created in the current working directory.
try (var pw = new PrintWriter(newFile)) { pw.write(joined.toString()); }
The joined values are written to the file.
$ gradle run -q
We run the application.
$ cat src/main/resources/numbers2.csv 21,43,54,76,98
We show the contents of the created file.
Java Collectors.joining
Tthe Collectors.joining
method returns a Collector
that concatenates the input elements, separated by the specified delimiter, in
encounter order.
package com.zetcode; import java.util.stream.Collectors; import java.util.stream.Stream; public class JoiningEx { public static void main(String[] args) { Stream<String> stream = Stream.of("Jan", "Peter", "Robert"); String names = stream.collect(Collectors.joining(" ")); System.out.println(names); } }
The example uses the stream API to concatenate three names.
$ java com/zetcode/JoiningEx.java Jan Peter Robert
Author
List all Java tutorials.
In this article we have covered StringJoiner
and
Collectors.joining
.