
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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Trace using cURL Command Example - cURL
- How to create MD5 digest in Notepad++ - NotepadPlusPlus
- Enable Native Dark Mode in Notepad++ - NotepadPlusPlus
- How to add days to a date in Python - Python
- Java Stream flatmap() Examples - Java
- See actual SharePoint error exception modify web.config - SharePoint
- Sublime Text 3 spell check shortcut - Sublime
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs - Python-Programs