
If you have a list of multiple Strings and you want to join them with a comma separator,
Example 1: Using Java 8 StringJoiner
import java.util.StringJoiner;
public class JavaStringJoinWithComma {
public static void main(String[] args) {
String string1 = "One";
String string2 = "Two";
String string3 = "Three";
StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add(string1).add(string2).add(string3);
System.out.println(stringJoiner.toString());
}
}
Output:
One,Two,Three
Example 2: Using Java 8 String.join() method
public class JavaStringJoinWithComma {
public static void main(String[] args) {
String string1 = "One";
String string2 = "Two";
String string3 = "Three";
String joinedString = String.join(",", string1,string2,string3);
System.out.println(joinedString);
}
}
One,Two,Three
Example 3: Using Java 8 Stream and Collectors
import java.util.ArrayList;
import java.util.stream.Collectors;
public class JavaStringJoinWithComma {
public static void main(String[] args) {
String string1 = "One";
String string2 = "Two";
String string3 = "Three";
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(string1);
arrayList.add(string2);
arrayList.add(string3);
String joinedString = arrayList.stream().collect(Collectors.joining(","));
System.out.println(joinedString);
}
}
-
Have Questions? Post them here!
More Posts related to Java,
- How to get Java Thread name Example [Program]
- Java 8: Get First and Last Date of the Week for Given Date
- Convert String to int in Java
- How to Get List of All Country Codes in Java Using Locale Class
- Convert Multidimensional Array toString In Java
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Create a Zip file using Java Code programmatically
- [Fix] java.net.MalformedURLException: unknown protocol
- [fix] Java JDBC ConnectException: Connection refused
- Read Json File and Convert to Java Object using Jackson
- list of jars required for hibernate 4.x.x
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java: The value of the local variable string is not used
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Run SQL Script file using Java JDBC Code Example
- Remove Trailing zeros BigDecimal Java
- Java JDBC IN Clause Example with PreparedStatement MySQL
- Convert Java List to Json String using Jackson
- Java 8 foreach loop code examples
- error: file not found: HelloWorld.java
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Struts 2 Hello World Example in Eclipse
More Posts:
- Java TLSv1.3 protocol code example using SSLSocket - Java
- How to List all Packages installed using pip [Python] - Python
- Download Google Chrome setup exe file using PowerShell - Powershell
- Find Restroom Near Me - Closest Toilet Nearby Using Your Current Location - HowTos
- How to check installed Tomcat version - Tomcat
- Align left align text in Bootstrap - Bootstrap
- 403 forbidden error for Image - PHP
- [Mac] Find a file using filename in macOS Terminal - MacOS