StringJoiner
StringJointer was added to Java 8 in java.util package and is mostly used to add a delimiter between a sequence of Strings/Characters to join them.
Example of StringJoiner
package org.code2care.examples;
import java.util.StringJoiner;
public class StringJoinerExample1 {
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add("1");
stringJoiner.add("2");
stringJoiner.add("3");
stringJoiner.add("4");
System.out.println(stringJoiner.toString());
}
}
Output:
StringBuilder
StringBuilder is a legacy class available in Java since version 5 which is used to useful for dynamically constructing strings by appending, inserting, or modifying characters.
Example of StringBuilder
package org.code2care.examples;
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("1");
stringBuilder.append(",");
stringBuilder.append("2");
stringBuilder.append(",");
stringBuilder.append("3");
stringBuilder.append(",");
stringBuilder.append("4");
String result = stringBuilder.toString();
System.out.println(result);
}
}
Output:
StringJoiner vs StringBuilder
StringJoiner | StringBuilder |
---|---|
Join strings with a specific delimiter. | Build and manipulate strings efficiently. |
Available since Java 8 | Available since Java 5 |
StringJoiner are not mutable | StringBuilder are mutable. |
Specified during initialization. | Appended using append method. |
Join elements from a collection. | Dynamically build strings with append or insert operations. |
Efficient for joining strings with a delimiter. | Efficient for dynamic string manipulations. |
StringJoiner are Tread-Safe | StringBuilder are not Thread-Safe |
-
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:
- 21 Essential Mac Terminal Shortcuts for Devs and DevOps to Boost Productivity - MacOS
- error CAML Query containing special characters - SharePoint
- Jupyter Notebook Markup Cell Table Example - Python
- How to Force Quit Microsoft Excel Application on Mac - Microsoft
- How to install Microsoft OneDrive on Mac Sonoma 14 - MacOS
- How to close all tabs of Notepad++? - NotepadPlusPlus
- Word wrap text in Notepad++ - NotepadPlusPlus
- Reading JSON file in Python with Examples - Python