While everyone was looking at the lambda expression and Stream API, JDK quietly sneaked some of the exciting methods on its API. There are a lot of hidden gems on JDK 8 and I have uncovered many of them already in this blog and today we'll talk about one of such gems which you can use in your day-to-day programming activities like joining much String together. The Java 8 has added a new class called StringJoiner to join Strings. The java.util.StringJoiner can be used to join any number of arbitrary String, a list of String, or an array of String in Java. You can choose any delimiter to join String like comma, pipe, colon, or semi-colon. This class also allows you to specify a prefix and suffix while joining two or more String in Java.
In order to join Strings, you first create an instance of StringJoiner class. While creating the instance, you provide the delimiter, a String or character, which will be used between Strings while joining them like you can pass comma as a delimiter to create a comma-separated String or pipe to create a pipe-delimited String.
In this article, you will see some examples of StringJoiner to learn how to join String in Java 8.
Some of the readers may be curious why do you need a new StringJoiner class if you already have StringBuffer and StringBuilder classes to concatenate String, which is nothing but joining.
Well, you can certainly use the StringBuffer and StringBuilder class to join String in Java and that's what Java developers do prior to Java 8
But, StringJoiner provides a much cleaner and capable interface to join Strings. You don't need to write logic to start adding comma only after the first element and not to add after the last element, which Java programmers used to do while joining String in Java 6 or JDK 7.
Though StringJoiner is just one of the hidden gems of the Java SE 8 release, there are many more day-to-day useful features that are hidden behind lambda expressions and streams like CompletableFuture. You can further see these Java Functional Programming and Stream Courses to learn more about useful features like Lambda expression and Stream in a quick time.
You can see that StringJoiner has joined all String you have added to it. You don't need to loop through a list of String anymore.
This code may look very similar to the code you may have written using StringBuffer but StringJoiner is very different from StringJoiner.
In the case of StringBuffer or StringBuilder, you need to explicitly call the append(",") to join String by a comma but, here, once you tell StringJoiner about delimiter you are done.
No need to call any function or write special logic, except adding String.
You can further shorten the above code in one line because StringJoiner allows fluent API as shown below:
You can also provide prefix and suffix String to StringJoiner which can be used to enclose String like by giving parenthesis as prefix and suffix you can enclose String as shown in our third example below:
You can see in this example, we have enclosed the comma-separated String with an opening and closing braces by supplying them as prefix and suffix.
One of the common use cases of this feature is dynamically generating IP address as shown in our fourth example below:
You can see the nice and clean IP address generated by supplying opening and closing brackets as prefix and suffix and dot as a separator. To be honest, these are just the tip of the iceberg in terms of both StringJoiner and Java 8 features.
I suggest you look at a comprehensive Java course like The Complete Java MasterClass which covers almost everything about Java SE 8. This will allow you to get the full benefit of new API enhancement and Java 8 features in your day-to-day programming.
That's all about how to use StringJoiner in Java 8 to Join multiple Strings. There is another alternative, you can use String.join() as well to join String. It internally uses StringJoiner for joining String but it's more versatile as it provides another overloaded version of String.join() to join elements from a String array or list of String.
Related Java 8 Tutorials and Examples you may like
Thanks for reading this article so far. If you like this Java 8 StringJoiner tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P.S.: If you just want to learn more about new features in Java 8 then please see these Java 8 Programming tutorials and courses from Udemy. It explains all the important features of Java 8 e.g. lambda expressions, streams, functional interfaces, Optional, new Date Time API, and other miscellaneous changes.
In order to join Strings, you first create an instance of StringJoiner class. While creating the instance, you provide the delimiter, a String or character, which will be used between Strings while joining them like you can pass comma as a delimiter to create a comma-separated String or pipe to create a pipe-delimited String.
In this article, you will see some examples of StringJoiner to learn how to join String in Java 8.
Some of the readers may be curious why do you need a new StringJoiner class if you already have StringBuffer and StringBuilder classes to concatenate String, which is nothing but joining.
Well, you can certainly use the StringBuffer and StringBuilder class to join String in Java and that's what Java developers do prior to Java 8
But, StringJoiner provides a much cleaner and capable interface to join Strings. You don't need to write logic to start adding comma only after the first element and not to add after the last element, which Java programmers used to do while joining String in Java 6 or JDK 7.
Though StringJoiner is just one of the hidden gems of the Java SE 8 release, there are many more day-to-day useful features that are hidden behind lambda expressions and streams like CompletableFuture. You can further see these Java Functional Programming and Stream Courses to learn more about useful features like Lambda expression and Stream in a quick time.
How to join String by a comma in Java 8 - Example
Let's see our first example, which will join String by a comma to create a CSV String in Java 8 using the StringJoiner class. In this example, we join arbitrary String like Java, C++, Python, and Ruby to form a comma-separated String.// Creating a StringJoiner with delimiter as comma StringJoiner joiner = new StringJoiner(","); joiner.add("Java"); joiner.add("C++"); joiner.add("Python"); joiner.add("Ruby"); String text = joiner.toString(); System.out.println("comma separated String: " + text); Output comma separated String: Java,C++,Python,Ruby
You can see that StringJoiner has joined all String you have added to it. You don't need to loop through a list of String anymore.
This code may look very similar to the code you may have written using StringBuffer but StringJoiner is very different from StringJoiner.
In the case of StringBuffer or StringBuilder, you need to explicitly call the append(",") to join String by a comma but, here, once you tell StringJoiner about delimiter you are done.
No need to call any function or write special logic, except adding String.
You can further shorten the above code in one line because StringJoiner allows fluent API as shown below:
You can see how you can join multiple String in just one line using StringJoiner and fluent API. If you are new to fluent API and interested in writing your own, you should check these Java design pattern courses on Udemy which talk about a software architecture approach for creating readable, intuitive, and easy-to-understand code.String CSV = new StringJoiner(",").add("Scala") .add("Haskell") .add("Lisp").toString(); System.out.println("CSV: " + CSV); Output CSV: Scala,Haskell,Lisp
You can also provide prefix and suffix String to StringJoiner which can be used to enclose String like by giving parenthesis as prefix and suffix you can enclose String as shown in our third example below:
String text = new StringJoiner(",", "(", ")") .add("Car Insurance") .add("Health Insurance") .add("Life Insurance").toString(); System.out.println("Insurance: " + text); Output Insurance: (Car Insurance,Health Insurance,Life Insurance)
You can see in this example, we have enclosed the comma-separated String with an opening and closing braces by supplying them as prefix and suffix.
One of the common use cases of this feature is dynamically generating IP address as shown in our fourth example below:
String text = new StringJoiner(".", "[", "]") .add("192") .add("168") .add("2") .add("81").toString(); System.out.println("IP address: " + text); Output IP address: [192.168.2.81]
You can see the nice and clean IP address generated by supplying opening and closing brackets as prefix and suffix and dot as a separator. To be honest, these are just the tip of the iceberg in terms of both StringJoiner and Java 8 features.
I suggest you look at a comprehensive Java course like The Complete Java MasterClass which covers almost everything about Java SE 8. This will allow you to get the full benefit of new API enhancement and Java 8 features in your day-to-day programming.
That's all about how to use StringJoiner in Java 8 to Join multiple Strings. There is another alternative, you can use String.join() as well to join String. It internally uses StringJoiner for joining String but it's more versatile as it provides another overloaded version of String.join() to join elements from a String array or list of String.
Related Java 8 Tutorials and Examples you may like
- 5 Free Courses to learn Java 8 and 9 (courses)
- 5 Books to Learn Java 8 Better? (read here)
- Java 8 String Documentation (docs)
- Top 5 Courses to become a full-stack Java developer (courses)
- Difference between Stream.map() and Stream.flatMap() in Java 8? (answer)
- 10 Examples of converting a List to Map in Java 8 (see here)
- 10 Free Courses to learn Spring Framework (courses)
- Java 8 Comparator Example (check here)
- Top 7 Courses to learn JVM in-depth (courses)
- Collection of best Java 8 tutorials (click here)
- 10 Examples of Stream in Java 8 (example)
- Difference between abstract class and interface in Java 8? (answer)
- How to sort the may by values in Java 8? (example)
- How to format/parse the date with LocalDateTime in Java 8? (tutorial)
- Top 5 Course to master Java 8 Programming (courses)
Thanks for reading this article so far. If you like this Java 8 StringJoiner tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P.S.: If you just want to learn more about new features in Java 8 then please see these Java 8 Programming tutorials and courses from Udemy. It explains all the important features of Java 8 e.g. lambda expressions, streams, functional interfaces, Optional, new Date Time API, and other miscellaneous changes.
And now is the quiz time, what is difference between joining String and concatenating String using Plus operator in Java?
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.