Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java StringJoiner

Java 8 StringJoiner joins strings with delimiter, prefix and suffix. Learn its usage with examples and differences with StringBuilder.

Lokesh Gupta

February 7, 2023

Java String class
Java String
Java 8 StringJoiner Example

Learn to use StringJoiner class (introduced in Java 8) to join strings in different ways. We can use it to join strings with a delimiter, and use prefix and/or suffix characters around the final string.

1. Creating StringJoiner

We can create an instance of StringJoiner in two ways. The first constructor takes only the delimiter, and the second constructor takes the additional prefix and suffix strings.

    String DELIMITER = ",";
    String PREFIX = "[";
    String SUFFIX = "]";

    StringJoiner stringJoinerWithDelimiter = new StringJoiner(DELIMITER);
    StringJoiner stringJoinerWithPrefixSuffix = new StringJoiner(DELIMITER, PREFIX, SUFFIX);

We cannot pass NULL arguments, they will result in NullPointerException.

Assertions.assertThrows(NullPointerException.class, () -> {
  new StringJoiner(null);
});

2. Default Values

By default, if we do not add any string to the joiner, it returns a default empty string. The empty string depends on the constructor used to create the StringJoiner instance.

  • If only the delimiter is used, the default value is an empty string.
  • If prefix and suffix have been used, the default value is a string joined by prefix + suffix.
Assertions.assertEquals("", stringJoinerWithDelimiter.toString());
Assertions.assertEquals("[]", stringJoinerWithPrefixSuffix.toString());

We can change the default string using the setEmptyValue() method.

stringJoinerWithDelimiter.setEmptyValue("BLANK");

Assertions.assertEquals("BLANK", stringJoinerWithDelimiter.toString());

The default prefix and suffix values are empty strings.

3. Adding Strings

We can use the StringJoiner.add() method to add strings that need to be joined.

stringJoinerWithDelimiter.add("alex");
stringJoinerWithDelimiter.add("brian");
stringJoinerWithDelimiter.add("charles");

Assertions.assertEquals("alex,brian,charles", stringJoinerWithDelimiter.toString());

If the prefix and suffix have been used, then the final string will contain them appended.

stringJoinerWithPrefixSuffix.add("alex");
stringJoinerWithPrefixSuffix.add("brian");
stringJoinerWithPrefixSuffix.add("charles");

Assertions.assertEquals("[alex,brian,charles]", stringJoinerWithPrefixSuffix.toString());

4. Merging StringJoiner Instances

It is possible that two parts of the application joined the strings, and now those need to be combined. Use StringJoiner.merge() method to merge two instances of StringJoiner to produce a single result.

When we merge two joiners using joiner1.merge(joiner2) –

  • The content of joiner2 is appended to joiner1.
  • Prefix and suffix are used for the joiner1.
  • Both joiners persist their delimiters.

Let us understand the merging process with an example. In the following Java program, we are creating two StringJoiner objects with different delimiters, prefixes and suffixes. The merged StringJoiner has prefix and suffix from the first joiner and delimiters from both joiners.

StringJoiner joiner1 = new StringJoiner(",", "[", "]");
StringJoiner joiner2 = new StringJoiner("-", "{", "}");

joiner1.add("a");
joiner1.add("b");

joiner2.add("1");
joiner2.add("2");

StringJoiner finalJoiner = joiner1.merge(joiner2);

Assertions.assertEquals("[a,b,1-2]", finalJoiner.toString());

5. StringJoiner in Collectors.joining()

The Collectors.joining() API is part of Java 8 Streams. It joins a stream of strings (or stream of primitives with their string value). The Collectors.joining() internally uses the StringJoiner class.

String result = Stream.of("a", "b", "c").collect(Collectors.joining(",", "[", "]"));

Assertions.assertEquals("[a,b,c]", result);

6. Difference between StringJoiner and StringBuilder

Using StringBuilder, we will append each string and delimiter in an alternate sequence.

StringBuilder builder = new StringBuilder();
String result = builder.append("a").append(",").append("b").append(",").append("c").toString();

Using StringJoiner with delimiter in the constructor, we only need to focus on strings to add. Delimiter will be added automatically.

stringJoinerWithDelimiter.add("a").add("b").add("c");

Overall, StringJoiner makes joining strings easy, as compared to StringBuilder.

7. Conclusion

In this Java 8 StringJoiner tutorial, we learned to create StringJoiner using constructors and merge it with another StringJoiner instance. We learned to join strings using the delimiters only, and with prefix and suffix strings. We also saw how the StringJoiner has been used in Stream API with Collectors.joining() method.

Overall, StringJoiner is much like StringBuilder and StringBuffer classes but with a more robust API for adding delimited strings, but it is unsuitable for other formatting tasks.

Happy Learning !!

References: StringJoiner Java Doc

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

Table of Contents

  • 1. Creating StringJoiner
  • 2. Default Values
  • 3. Adding Strings
  • 4. Merging StringJoiner Instances
  • 5. StringJoiner in Collectors.joining()
  • 6. Difference between StringJoiner and StringBuilder
  • 7. Conclusion
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java – Normalize Extra White Spaces in a String

Next

Left Pad a String with Spaces or Zeros in Java

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap