
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);
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Implementing Bubble Sort Algorithm using Java Program - Java
- Java Check Leap Year - Programs with Code Examples - Java
- Create Bootstrap carousel slider with Text - Bootstrap
- Online JSON Validator Tool - Tools
- Fix: No Internet Connection on iPhone WiFi Network - iOS
- Microsoft 365: How to Turn Off Delve in SharePoint Online for All Users - SharePoint
- Turn Off Google Analytics intelligence Alert Emails - Google
- Use Netbeans keyboard shortcuts in Android Studio - Android-Studio