Java 8 StringJoiner字符串连接器


在这篇简短的文章中,我们将介绍Java 8 StringJoiner特性,并将探索有哪些不同的特性以及如何更有效地使用Java 8 Stream API。
在日常编程活动中的一项非常常见的任务中加入多个字符串。在 Java 中没有直接连接多个 String 的方法(除了使用第三方 API)。Java 8引入了一个新类StringJoiner,可用于连接多个字符串。StringJoiner是一种 Collector,在这个类的帮助下,我们可以用特定的分隔符连接多个字符串,它还提供了向最终输出添加前缀和后缀的选项。本文重点介绍 Java 8 StringJoiner 类及其不同的特性。
 
使用分隔符

StringJoiner joiner = new StringJoiner(",");
    joiner.add(
"Sunday");
    joiner.add(
"Monday");
    joiner.add(
"Tuesday");

 System.out.println(joiner.toString());

 
使用分隔符、前缀和后缀

public static void joinerWithDelimiterWithPrefixAndSuffix(){
    StringJoiner joiner = new StringJoiner(",", "Prefix-", "-Suffix");
    joiner.add(
"Sunday");
    joiner.add(
"Monday");
    joiner.add(
"Tuesday");
    joiner.add(
"Wednesday");

   
//display output
    System.out.println(joiner.toString());
}
输出:
Prefix-Sunday-Monday-Tuesday-Wednesday-Suffix

 
多个 StringJoiner
使用 StringJoiner 的merge()方法合并多个StringJoiner:

   StringJoiner joiner1= new StringJoiner("|");
   StringJoiner joiner2= new StringJoiner(
";");
   StringJoiner joiner3= new StringJoiner(
";");

   joiner2.add(
"one");
   joiner2.add(
"two");
   joiner1.merge(joiner2);

   joiner3.add(
"three");
   joiner3.add(
"four");
   joiner1.merge(joiner3);

   System.out.println(joiner1.toString());
输出:

one;two|three;four
 
String.join()
StringJoiner由两个静态String.join方法内部使用:
String output= String.join("-", "2017","09","30");
2017-09-30
//output
List<String> list= Arrays.asList(
"one","two","three");
String output = String.join(
"-", list);

one-two-three
//output

 
Collectors.joining
StringJoiner可以很容易地与新的Java 8 收集器一起使用。这是使用Java 8 StringJoiner类的间接方式。
List<Customer> customers = Arrays.asList(
        new Customer("John", "Smith"),
        new Customer(
"Umesh", "Awasthi"),
        new Customer(
"Andy", "Flower")
);

final String customerJoin = customers.stream()
        .map(Customer::getFirstName)
        .collect(Collectors.joining(
","));

System.out.println(customerJoin);

输出:
John, Umesh, Andy
 
StringJoiner 和 setEmptyValue 方法
在上面的所有示例中,我们使用了toString()从 joiner 类中获取值的方法。您应该注意以下几点:
  1. 如果没有提供前缀和后缀并且字符串为空,则连接器将返回空字符串。
  2. 对于空字符串和前缀 ans suffix ,joiner 将返回一个包含前缀和后缀的字符串。

这在大多数情况下都有效,但有时,我们可能需要一个不同的默认值,以防字符串为空(我们不想要空字符串),StringJoiner 提供了setEmptyValue()帮助我们设置默认值的方法,以防字符串为空。让我们看一个例子:
import java.util.StringJoiner;

public class StringJoinerExample {

    public static void main(String args) {
        StringJoiner joiner = new StringJoiner(",");
        System.out.println(joiner.toString());
// empty String

        joiner.setEmptyValue(
"Default Value");

        System.out.println(joiner.toString());
//Default Value
    }
}

仅当StringJoiner为空时才返回默认值Default Value

import java.util.StringJoiner;

public class StringJoinerExample {

    public static void main(String[] args) {
        StringJoiner joiner = new StringJoiner(",");
        System.out.println(joiner.toString());
// empty String

        joiner.setEmptyValue(
"Default Value");
        joiner.add(
"Checking");
        joiner.add(
"setEmptyValue behaviour");
        System.out.println(joiner.toString());
//Checking,setEmptyValue behaviour
    }
}

 
StringJoiner和StringBuilder
使用Java 8 Stream API,StringJoiner 与StringBuilder 相比非常有用。让我们举个例子来理解使用SpringJoiner 和StringBuilder 之间的区别。
List<String> list = Arrays.asList("Foo","Bar");

//join string using StringJoiner
String output = list.stream().collect(Collectors.joining(
","));

//using StringBuilder

String collectoutput =
        list.stream().collect(Collector.of(StringBuilder::new,
                (stringBuilder, str) -> stringBuilder.append(str).append(
", "),
                StringBuilder::append,
                StringBuilder::toString));

 
概括
在这篇文章中,我们得到了Java8 StringJoiner的介绍。我们探索了 StringJoiner 类的各种功能以及如何使用它来连接多个字符串。我们也可以将 StringJoiner 视为一种收集器,在处理并行流时非常有用。源代码可在我们的GitHub 存储库中找到