原创  JAVA8 StringJoiner的使用以及字符拼接的各种写法

分类:java 2020-09-09T21:27:16    94人阅读   

一、使用StringJoiner

StringJoiner是Java8新出的一个类,用于构造由分隔符分隔的字符序列,并可选择性地从提供的前缀开始和以提供的后缀结尾。省的我们开发人员再次通过StringBuffer或者StingBuilder拼接。

 1、使用风格符分割

使用构造函数: 

java.util.StringJoiner.StringJoiner(CharSequence delimiter)

StringJoiner stringJoiner = new StringJoiner(",");
stringJoiner.add("one");
stringJoiner.add("two");
stringJoiner.add("three");
 
//结果: one,two,three
System.out.println(stringJoiner.toString());

2、带前缀和后缀

使用构造函数:

java.util.StringJoiner.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
StringJoiner stringJoinerWithPrefixSufix = new StringJoiner(",", "{", "}");
stringJoinerWithPrefixSufix.add("1");
stringJoinerWithPrefixSufix.add("2");
stringJoinerWithPrefixSufix.add("3");
//结果: {1,2,3}
System.out.println(stringJoinerWithPrefixSufix.toString());

二、使用String自带静态方法join

1、使用构造函数 :

String java.lang.String.join(CharSequence delimiter, CharSequence... elements)
String strWithJoiner = String.join("|", "one", "two", "three");
//结果: one|two|three
System.out.println(strWithJoiner);

2、使用构造函数:

String java.lang.String.join(CharSequence delimiter, Iterable elements)
List<String> alphabeLst = Arrays.asList("A", "B", "C");
strWithJoiner = String.join("|", alphabeLst);
//结果: A|B|C
System.out.println(strWithJoiner);

三、使用Java 8 Collectors.joining

1、使用:Collectors.joining()

List<String> list = Arrays.asList("java6", "java7", "java8");
 
String collectorJoiningResult = list.stream().map(element -> element).collect(Collectors.joining());
//结果: java6java7java8
System.out.println(collectorJoiningResult);

2、使用:Collectors.joining 带分隔符

String collectorJoiningResult = list.stream().map(element -> element).collect(Collectors.joining(" -> "));
//Result: java6 -> java7 -> java8
System.out.println(collectorJoiningResult);

3、使用Collectors.joining 带前缀后缀

Collector java.util.stream.Collectors.joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
String collectorJoiningResult = list.stream().map(element -> element).collect(Collectors.joining(" -> ", "[", "]"));
//Result: [java6 -> java7 -> java8]
System.out.println(collectorJoiningResult);

四、源代码

package com.javasampleapproach.stringjoiner;
 
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
 
public class Java8StringJoiner {
  public static void main(String[] args) {
 
    // I. StringJoiner
 
    System.out.println("### I. String Joiner");
    // -> 1. java.util.StringJoiner.StringJoiner(CharSequence delimiter)
    StringJoiner stringJoiner = new StringJoiner(",");
    stringJoiner.add("one");
    stringJoiner.add("two");
    stringJoiner.add("three");
 
    // Result: one,two,three
    System.out.println(stringJoiner.toString());
 
    // -> 2. java.util.StringJoiner.StringJoiner(CharSequence delimiter,
    // CharSequence prefix, CharSequence suffix)
    StringJoiner stringJoinerWithPrefixSufix = new StringJoiner(",", "{", "}");
    stringJoinerWithPrefixSufix.add("1");
    stringJoinerWithPrefixSufix.add("2");
    stringJoinerWithPrefixSufix.add("3");
    // Result: {1,2,3}
    System.out.println(stringJoinerWithPrefixSufix.toString());
 
    // II. String with static join function
    System.out.println("### II. String with static join function");
    // -> 1. String java.lang.String.join(CharSequence delimiter,
    // CharSequence... elements)
    String strWithJoiner = String.join("|", "one", "two", "three");
    // Result: one|two|three
    System.out.println(strWithJoiner);
 
    // -> 2. <String> List<String> java.util.Arrays.asList(String... a)
    List<String> alphabeLst = Arrays.asList("A", "B", "C");
    strWithJoiner = String.join("|", alphabeLst);
    // Result: A|B|C
    System.out.println(strWithJoiner);
 
    // III. Java 8 with Collectors.joining
    System.out.println("### III. Java 8 with Collectors.joining");
    // -> 1. Collector<CharSequence, ?, String>
    // java.util.stream.Collectors.joining()
    List<String> list = Arrays.asList("java6", "java7", "java8");
 
    String collectorJoiningResult = list.stream().map(element -> element).collect(Collectors.joining());
    // Result: java6java7java8
    System.out.println(collectorJoiningResult);
 
    // -> 2. Collectors.joining(CharSequence delimiter)
    collectorJoiningResult = list.stream().map(element -> element).collect(Collectors.joining(" -> "));
    // Result: java6 -> java7 -> java8
    System.out.println(collectorJoiningResult);
 
    // -> 3. Collectors.joining(CharSequence delimiter, CharSequence prefix,
    // CharSequence suffix)
    collectorJoiningResult = list.stream().map(element -> element).collect(Collectors.joining(" -> ", "[", "]"));
    // Result: [java6 -> java7 -> java8]
    System.out.println(collectorJoiningResult);
 
  }
}


运行结果:

### I. String Joiner
one,two,three
{1,2,3}
### II. String with static join function
one|two|three
A|B|C
### III. Java 8 with Collectors.joining
java6java7java8
java6 -> java7 -> java8
[java6 -> java7 -> java8]



分享到: