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,
- 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:
- How to view the Eclipse error log - Eclipse
- Rename git branch on Local and GitHub Remove using Command - Git
- Microsoft AI-900 Fundamental: A look at the Official Study Guide For Exam - Microsoft
- What is ValueError: math domain error and how to fix it - Python
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python] - Python
- Simple Login Page using Bootstrap - Bootstrap
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server - Java
- How to Display Excel Spreadsheet on SharePoint site - SharePoint