- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 233字
- 2021-07-02 19:01:58
String collectors
The string concatenation example can be written using the collector as follows:
//String Concatenation using non parameterized joining
String concatedString = streamSupplier.collect(Collectors.joining());
The factory method joining() will concatenate all the string elements of the stream into a string. There are other overloaded joining() methods, one accepting a delimiter to separate each element of the concatenated string. This comes in handy while generating special delimited text files such as CSVs:
//String Concatenation using joining with delimiter parameter
String delimitedString = streamSupplier.collect(Collectors.joining(","));
The joining() function also has an overloaded function with three parameters, which are delimiter, prefix, and suffix. The overloaded method has been designed in such a way that the developer does not need to worry about the element being iterated in the stream. The method smartly appends a suffix in the end instead of the delimiter:
//String Concatenation using joining with delimiter parameter
String concatString = streamSupplier.collect(Collectors.joining(",","[","]"));
The joining() method not only provides the simplest optimized way to concatenate the strings, but high performance as well. As you will notice the strings can be concatenated using the reduce() method as well, but strings are immutable and hence accumulators concatenating strings tend to copy the string each time. While one can also use the forEach() method along with a Lambda function to append the StringBuffer or StringBuilder, all these options do not perform as well as the joining() method of the collectors.
- Mastering Entity Framework Core 2.0
- Spring Cloud Alibaba核心技術(shù)與實戰(zhàn)案例
- Web Scraping with Python
- Xcode 7 Essentials(Second Edition)
- .NET 4.0面向?qū)ο缶幊搪劊夯A(chǔ)篇
- The Complete Coding Interview Guide in Java
- 深入理解Android:Wi-Fi、NFC和GPS卷
- Visual C++開發(fā)入行真功夫
- Python深度學(xué)習(xí):模型、方法與實現(xiàn)
- Web Developer's Reference Guide
- C++ System Programming Cookbook
- UML軟件建模
- PHP+MySQL Web應(yīng)用開發(fā)教程
- Getting Started with JUCE
- Java網(wǎng)絡(luò)編程實用精解