Java8 - 更优雅的字符串连接(join)收集器 Collectors.joining

版权声明:知识诚无价,分享价更高。 https://blog.csdn.net/u013955940/article/details/83054037

Java8中的字符串连接收集器

在JDK8中,可以采用函数式编程(使用 Collectors.joining 收集器)的方式对字符串进行更优雅的连接。
Collectors.joining 收集器 支持灵活的参数配置,可以指定字符串连接时的 分隔符前缀后缀 字符串。

代码参考如下:

// 定义人名数组
final String[] names = {"Zebe", "Hebe", "Mary", "July", "David"};
Stream<String> stream1 = Stream.of(names);
Stream<String> stream2 = Stream.of(names);
Stream<String> stream3 = Stream.of(names);
// 拼接成 [x, y, z] 形式
String result1 = stream1.collect(Collectors.joining(", ", "[", "]"));
// 拼接成 x | y | z 形式
String result2 = stream2.collect(Collectors.joining(" | ", "", ""));
// 拼接成 x -> y -> z] 形式
String result3 = stream3.collect(Collectors.joining(" -> ", "", ""));
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);

程序输出结果如下:

[Zebe, Hebe, Mary, July, David]
Zebe | Hebe | Mary | July | David
Zebe -> Hebe -> Mary -> July -> David

一般的做法(不推荐)

在JAVA8出现之前,我们通常使用循环的方式来拼接字符串,这样做不仅代码冗长丑陋,而且需要仔细阅读代码才知道代码的功能,例如下面的代码:

final String[] names = {"Zebe", "Hebe", "Mary", "July", "David"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < names.length; i++) {
    if (builder.length() > 1) {
        builder.append(",");
    }
    builder.append(names[i]);
}
System.out.println(builder.toString());

本文首发于个人独立博客,文章链接:http://www.zebe.me/java-8-string-join-collector

猜你喜欢

转载自blog.csdn.net/u013955940/article/details/83054037