()
is a practical method provided by Java, which is used to concatenate multiple strings into a string according to the specified delimiter. This method was introduced in Java 8, greatly simplifying the operation of string splicing.
1. Method definition
public static String join(CharSequence delimiter, CharSequence... elements) public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Parameter description:
-
delimiter
: Delimiter, used to separate each string. -
elements
:- Variable parameter list (
CharSequence...
): Multiple strings. - Iterable object (
Iterable
):likeList
orSet
etc.
- Variable parameter list (
Return value:
- Returns a string concatenated by a delimiter.
2. Basic usage
2.1 Splicing multiple strings
- use
()
Splicing a fixed number of strings:
String result = (", ", "Apple", "Banana", "Cherry"); (result); // Output: Apple, Banana, Cherry
2.2 Stitching strings in collections
- use
()
Strings in the stitching collection:
List<String> fruits = ("Apple", "Banana", "Cherry"); String result = (", ", fruits); (result); // Output: Apple, Banana, Cherry
3. Usage scenarios and examples
3.1 Dynamically build comma-separated strings
public class JoinExample { public static void main(String[] args) { String[] items = {"Java", "Python", "C++"}; String result = (", ", items); (result); // Output: Java, Python, C++ } }
3.2 Stitching strings in collections
import .*; public class JoinExample { public static void main(String[] args) { List<String> programmingLanguages = ("Java", "Python", "JavaScript"); String result = (" | ", programmingLanguages); (result); // Output: Java | Python | JavaScript } }
3.3 Combined with Stream API
When you need to deal with complex collections, you can use it in conjunction with the Stream API:
import .*; import .*; public class JoinExample { public static void main(String[] args) { List<String> cities = ("New York", "London", "Tokyo"); String result = () .filter(city -> () > 5) // Filter cities with lengths greater than 5 .collect((", ")); (result); // Output: New York, London } }
3.4 Processing of empty collections
If the incoming is an empty set,()
An empty string will be returned:
import .*; public class JoinExample { public static void main(String[] args) { List<String> emptyList = (); String result = (", ", emptyList); (()); // Output: true } }
4. Features and advantages
- Simple and easy to use:
- No explicit looping and splicing is required, simplifying the code.
- Strong flexibility:
- Supports variable parameters and collections (
Iterable
)。
- Supports variable parameters and collections (
- Avoid performance issues with string concatenation:
- Compared with manual splicing (such as using
+
),()
More efficient, especially when there are many strings.
- Compared with manual splicing (such as using
5. Things to note
-
The separator cannot be
null
:- If the delimiter is
null
, will be thrownNullPointerException
。 - For example:
String result = (null, "A", "B"); // An exception will be thrown
- If the delimiter is
-
Supports empty strings:
- If the element contains an empty string or
null
, they will be processed as normal strings. - For example:
String result = (", ", "Apple", "", "Cherry"); (result); // Output: Apple, Cherry
- If the element contains an empty string or
-
deal with
null
Collection or array:- If the set or array itself is
null
, will be thrownNullPointerException
。 - The solution is to check whether it is
null
:List<String> list = null; String result = (list != null) ? (", ", list) : ""; (result); // The output is an empty string
- If the set or array itself is
6. Source code analysis
()
Internal implementation:
- For collection types, each element in the collection is traversed through an iterator, inserting the separator between each two elements.
- For the variable parameter list, the underlying layer also implements splicing through loops.
Sample source code snippet:
public static String join(CharSequence delimiter, CharSequence... elements) { (delimiter); // Make sure the delimiter is not null StringJoiner joiner = new StringJoiner(delimiter); for (CharSequence cs : elements) { (cs); } return (); }
7. Comparison with other methods
7.1 with manual splicing
- Manual stitching requires handling of delimiter addition and boundary issues.
-
()
Automatically process separators.
Manual splicing example:
String[] words = {"Java", "Python", "C++"}; StringBuilder sb = new StringBuilder(); for (int i = 0; i < ; i++) { (words[i]); if (i < - 1) { (", "); } } (()); // Output: Java, Python, C++
()
Alternative:
String result = (", ", "Java", "Python", "C++"); (result); // Output: Java, Python, C++
7.2 with ()
()
is a tool provided by the Java Stream API for splicing elements in streams.
method | use | Example |
---|---|---|
() | Suitable for stitching of ordinary arrays or collections. | Simple splicing operation. |
() | Used to process data in a stream. | Combined with filtering, sorting and other stream operations. |
Example:
List<String> list = ("A", "B", "C"); // use ()String result1 = (", ", list); (result1); // Output: A, B, C // use ()String result2 = ().collect((", ")); (result2); // Output: A, B, C
8. Summary
()
is a simple tool for string splicing, providing a powerful and concise API.
Main features:
- Supports collection and array stitching.
- Automatically process separators without additional logic.
- It is more suitable for simple scenarios, but not for complex flow operations.
Applicable scenarios:
- Splice fixed list of strings.
- Stitching strings in the collection.
- Replace manual splicing logic to improve code readability and efficiency.
This is the end of this article about the use of java () in the summary. For more related java () content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!